使用正则表达式修改文本文件(awk?)
我有一个具有以下格式的文本文件:
line 450
10876 -022.6421047 -070.1866390 000882 23362.47 99 000000.00 10 202246.0
10877 -022.6421090 -070.1866412 000882 23363.42 99 000000.00 10 202247.0
10878 -022.6421090 -070.1866412 000882 23363.93 99 000000.00 10 202248.0
10879 -022.6421090 -070.1866412 000882 23363.68 99 000000.00 10 202249.0
10880 -022.6421090 -070.1866412 000882 23363.72 99 000000.00 10 202250.0
line 460
10872 -022.6420829 -070.1866339 000882 23424.83 99 000000.00 10 202242.0
10873 -022.6420889 -070.1866373 000882 23413.99 99 000000.00 10 202243.0
10874 -022.6420945 -070.1866378 000882 23393.97 99 000000.00 10 202244.0
10875 -022.6421000 -070.1866369 000882 23375.70 99 000000.00 10 202245.0
我需要做的是删除空白行,并在每个“第 XXX 行”行之间的每个数字块中连接 XXX。然后,删除以“line”开头的每一行。为了清楚起见,以下是所需输出文件的示例:
10876 -022.6421047 -070.1866390 000882 23362.47 99 000000.00 10 202246.0 450
10877 -022.6421090 -070.1866412 000882 23363.42 99 000000.00 10 202247.0 450
10878 -022.6421090 -070.1866412 000882 23363.93 99 000000.00 10 202248.0 450
10879 -022.6421090 -070.1866412 000882 23363.68 99 000000.00 10 202249.0 450
10880 -022.6421090 -070.1866412 000882 23363.72 99 000000.00 10 202250.0 450
10872 -022.6420829 -070.1866339 000882 23424.83 99 000000.00 10 202242.0 460
10873 -022.6420889 -070.1866373 000882 23413.99 99 000000.00 10 202243.0 460
10874 -022.6420945 -070.1866378 000882 23393.97 99 000000.00 10 202244.0 460
10875 -022.6421000 -070.1866369 000882 23375.70 99 000000.00 10 202245.0 460
I've got a textfile with the following format:
line 450
10876 -022.6421047 -070.1866390 000882 23362.47 99 000000.00 10 202246.0
10877 -022.6421090 -070.1866412 000882 23363.42 99 000000.00 10 202247.0
10878 -022.6421090 -070.1866412 000882 23363.93 99 000000.00 10 202248.0
10879 -022.6421090 -070.1866412 000882 23363.68 99 000000.00 10 202249.0
10880 -022.6421090 -070.1866412 000882 23363.72 99 000000.00 10 202250.0
line 460
10872 -022.6420829 -070.1866339 000882 23424.83 99 000000.00 10 202242.0
10873 -022.6420889 -070.1866373 000882 23413.99 99 000000.00 10 202243.0
10874 -022.6420945 -070.1866378 000882 23393.97 99 000000.00 10 202244.0
10875 -022.6421000 -070.1866369 000882 23375.70 99 000000.00 10 202245.0
What I need to do is delete the blanklines, and to every block of numbers between each "line XXX" line, concatenate the XXX. Then, delete every line starting with "line". For clarity, here is an example of the required output file:
10876 -022.6421047 -070.1866390 000882 23362.47 99 000000.00 10 202246.0 450
10877 -022.6421090 -070.1866412 000882 23363.42 99 000000.00 10 202247.0 450
10878 -022.6421090 -070.1866412 000882 23363.93 99 000000.00 10 202248.0 450
10879 -022.6421090 -070.1866412 000882 23363.68 99 000000.00 10 202249.0 450
10880 -022.6421090 -070.1866412 000882 23363.72 99 000000.00 10 202250.0 450
10872 -022.6420829 -070.1866339 000882 23424.83 99 000000.00 10 202242.0 460
10873 -022.6420889 -070.1866373 000882 23413.99 99 000000.00 10 202243.0 460
10874 -022.6420945 -070.1866378 000882 23393.97 99 000000.00 10 202244.0 460
10875 -022.6421000 -070.1866369 000882 23375.70 99 000000.00 10 202245.0 460
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
awk
(假设您的输入位于file.txt
中并且结果位于 stdout 上):With
awk
(assuming your input is infile.txt
and the result is on stdout):在 Perl 中,您可以执行以下操作:
Ideone Link
In Perl you can do:
Ideone Link
Sed 可能不是完成这项工作的最佳工具,但
请注意,除了一行数字之外,所有数字行都以空格结尾;该解决方案假设这是一个拼写错误,并且他们都应该这样做。如果没有人应该,那么使用这个:
如果有些可能,有些可能不,那么请谨慎行事:
Sed probably isn't the best tool for this job, but
Note that all but one of your numeric lines ends with a space; this solution assumes that that was a typo and that they all should. If none should, then use this:
and if some might and some might not, then play it safe: