sed 将 .txt 中的文本移动到下一行

发布于 2025-01-04 20:31:05 字数 820 浏览 1 评论 0原文

我正在尝试解析一个如下所示的文本文件:

EMPIRE,STATE,BLDG,CO,494202320000008,336,5,AVE,ENT,NEW,YORK,NY,10003,N,3/1/2012,TensionCode,VariableICAP,PFJICAP,Residential,%LBMPZone,L,9,146.0,,,10715.0956,,,--,,0,,,J,TripNumber,ServiceClass,PreviousAccountNumber,MinMonthlyDemand,TODCode,Profile,Tax,Muni,41,39,00000000000000,9952,54,Y,Non-Taxable,--,FromDate,ToDate,Use,Demand,BillAmt,12/29/2011,1/31/2012,4122520,6,936.00,$293,237.54

我想看到的是堆叠的数据

- EMPIRE STATE BLDG CO
- 494202320000008
- 336 5 AVE ENT
- NEW YORK NY

等等。如果有的话,在每个逗号之后,我希望后面的文本转到新的 txt 行。最终,关于最后一行指出从前开始的日期,我希望将其放在 txt 文件中,就像

- From Date  ToDate    use     Demand   BillAmt
- 12/29/2011 1/31/2012 4122520 6,936.00 $293,237.54.

我在 Windows XP 计算机上使用 cygwin 一样。预先感谢您提供的任何帮助。

I am trying to parse out a text file that looks like the following:

EMPIRE,STATE,BLDG,CO,494202320000008,336,5,AVE,ENT,NEW,YORK,NY,10003,N,3/1/2012,TensionCode,VariableICAP,PFJICAP,Residential,%LBMPZone,L,9,146.0,,,10715.0956,,,--,,0,,,J,TripNumber,ServiceClass,PreviousAccountNumber,MinMonthlyDemand,TODCode,Profile,Tax,Muni,41,39,00000000000000,9952,54,Y,Non-Taxable,--,FromDate,ToDate,Use,Demand,BillAmt,12/29/2011,1/31/2012,4122520,6,936.00,$293,237.54

what I would like to see is the data stacked

- EMPIRE STATE BLDG CO
- 494202320000008
- 336 5 AVE ENT
- NEW YORK NY

and so on. If anything, after each comma I would want the text following to go to a new txt line. Ultimatly in regards to the last line where it states date from forward, I would like to have it in a txt file like

- From Date  ToDate    use     Demand   BillAmt
- 12/29/2011 1/31/2012 4122520 6,936.00 $293,237.54.

I am using cygwin on a windows XP machine. Thank you in advance for any assistance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉月流沐 2025-01-11 20:31:05

将最后一行放入单独的文件中:

echo -e "From Date\tToDate\tuse\tDemand\tBillAmt" > lastlinefile.txt
cat originalfile.txt | sed 's/,FromDate/~Fromdate/' | awk -v FS="~" '{print $2}' | sed 's/FromDate,ToDate,use,Demand,BillAmt,//' | sed 's/,/\t/' >> lastlinefile.txt

对于其余部分:

cat originalfile.txt | sed -r 's/,Fromdate[^\n]+//' | sed 's/,/\n/' | sed -r 's/$/\n\n' > nocommas.txt

就第二个命令中的第一个“\n”而言,您的里程可能会有所不同。如果它不能正常工作,请将其替换为空格(假设您的数据没有空格)。

或者,如果您愿意,可以使用 shell 脚本来操作文件并将其拆分:

#!/bin/bash
if [ -z "$1" ]
then echo "Usage: $0 filename.txt; exit; fi

echo -e "From Date\tToDate\tuse\tDemand\tBillAmt" > "$1_lastline.txt"
cat "$1" | sed 's/,FromDate/~Fromdate/' | awk -v FS="~" '{print $2}' | sed 's/FromDate,ToDate,use,Demand,BillAmt,//' | sed 's/,/\t/' >> "$1_lastline.txt"

cat "$1" | sed -r 's/,Fromdate[^\n]+//' | sed 's/,/\n/' | sed -r 's/$/\n\n' > "$1_fixed.txt"

只需将其粘贴到文件中并运行即可。我已经使用 Cygwin 好几年了...您可能必须先对其进行 chmod +x file

For getting the last line into a separate file:

echo -e "From Date\tToDate\tuse\tDemand\tBillAmt" > lastlinefile.txt
cat originalfile.txt | sed 's/,FromDate/~Fromdate/' | awk -v FS="~" '{print $2}' | sed 's/FromDate,ToDate,use,Demand,BillAmt,//' | sed 's/,/\t/' >> lastlinefile.txt

For the rest:

cat originalfile.txt | sed -r 's/,Fromdate[^\n]+//' | sed 's/,/\n/' | sed -r 's/$/\n\n' > nocommas.txt

Your mileage may vary as far as the first '\n' is concerned in the second command. It if doesn't work properly replace it with a space (assuming your data doesn't have spaces).

Or, if you like, a shell script to operate on a file and split it:

#!/bin/bash
if [ -z "$1" ]
then echo "Usage: $0 filename.txt; exit; fi

echo -e "From Date\tToDate\tuse\tDemand\tBillAmt" > "$1_lastline.txt"
cat "$1" | sed 's/,FromDate/~Fromdate/' | awk -v FS="~" '{print $2}' | sed 's/FromDate,ToDate,use,Demand,BillAmt,//' | sed 's/,/\t/' >> "$1_lastline.txt"

cat "$1" | sed -r 's/,Fromdate[^\n]+//' | sed 's/,/\n/' | sed -r 's/$/\n\n' > "$1_fixed.txt"

Just paste it into a file and run it. It's been years since I used Cygwin... you may have to chmod +x file it first.

昔日梦未散 2025-01-11 20:31:05

我根据您想要文件的方式为您提供两个答案。上一个答案将其分成两个文件,这个将其全部保存在一个文件中,格式如下:

EMPIRE
STATE
BLDG
CO
494202320000008
336
5
AVE
ENT
NEW
YORK
NY
From Date  ToDate    use     Demand   BillAmt
12/29/2011 1/31/2012 4122520 6,936.00 $293,237.54.

这是我对您设置的分隔符所能做的最好的事情。如果您留下类似“EMPIRE STATE BUILDING CO,494202320000008,336 5 AVE ENT,NEW YORK,NY”之类的内容,那就容易多了。

#!/bin/bash
if [ -z "$1" ]
then echo "Usage: $0 filename.txt; exit; fi

cat "$1" | sed 's/,FromDate/~Fromdate/' | awk -v FS="~" '{gsub(",","\n",$1);print $1;print "FromDate\tToDate\tuse\tDemand\tBillAmt";gsub("FromDate,ToDate,use,Demand,BillAmt","",$2);gsub(",","\t",$2);print $2}' >> "$1_fixed.txt" 

再次,只需将其粘贴到文件中并从 Cygwin 运行它: ./filename.sh

I'm providing you two answers depending on how you wanted the file. The previous answer split it into two files, this one keeps it all in one file in the format:

EMPIRE
STATE
BLDG
CO
494202320000008
336
5
AVE
ENT
NEW
YORK
NY
From Date  ToDate    use     Demand   BillAmt
12/29/2011 1/31/2012 4122520 6,936.00 $293,237.54.

That's the best I can do with the delimiters have you set in place. If you'd have left it something like "EMPIRE STATE BUILDING CO,494202320000008,336 5 AVE ENT,NEW YORK,NY" it'd be a lot easier.

#!/bin/bash
if [ -z "$1" ]
then echo "Usage: $0 filename.txt; exit; fi

cat "$1" | sed 's/,FromDate/~Fromdate/' | awk -v FS="~" '{gsub(",","\n",$1);print $1;print "FromDate\tToDate\tuse\tDemand\tBillAmt";gsub("FromDate,ToDate,use,Demand,BillAmt","",$2);gsub(",","\t",$2);print $2}' >> "$1_fixed.txt" 

again, just paste it into a file and run it from Cygwin: ./filename.sh

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文