sed 将 .txt 中的文本移动到下一行
我正在尝试解析一个如下所示的文本文件:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将最后一行放入单独的文件中:
对于其余部分:
就第二个命令中的第一个“\n”而言,您的里程可能会有所不同。如果它不能正常工作,请将其替换为空格(假设您的数据没有空格)。
或者,如果您愿意,可以使用 shell 脚本来操作文件并将其拆分:
只需将其粘贴到文件中并运行即可。我已经使用 Cygwin 好几年了...您可能必须先对其进行
chmod +x file
。For getting the last line into a separate file:
For the rest:
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:
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.我根据您想要文件的方式为您提供两个答案。上一个答案将其分成两个文件,这个将其全部保存在一个文件中,格式如下:
这是我对您设置的分隔符所能做的最好的事情。如果您留下类似“EMPIRE STATE BUILDING CO,494202320000008,336 5 AVE ENT,NEW YORK,NY”之类的内容,那就容易多了。
再次,只需将其粘贴到文件中并从 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:
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.
again, just paste it into a file and run it from Cygwin:
./filename.sh