使用正则表达式修改文本文件(awk?)

发布于 2024-10-21 05:30:30 字数 1546 浏览 2 评论 0原文

我有一个具有以下格式的文本文件:

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 技术交流群。

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

发布评论

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

评论(5

尤怨 2024-10-28 05:30:30

使用 awk (假设您的输入位于 file.txt 中并且结果位于 stdout 上):

awk '
/^line/ {number = $2}
/^[0-9]/ {print $0, number}
' file.txt

With awk (assuming your input is in file.txt and the result is on stdout):

awk '
/^line/ {number = $2}
/^[0-9]/ {print $0, number}
' file.txt
捂风挽笑 2024-10-28 05:30:30

在 Perl 中,您可以执行以下操作:

perl -nle 'if(/^line/){($l = $_)=~s/\D//g;}elsif(/^\d/){print "$_ $l"}' file

Ideone Link

In Perl you can do:

perl -nle 'if(/^line/){($l = $_)=~s/\D//g;}elsif(/^\d/){print "$_ $l"}' file

Ideone Link

如痴如狂 2024-10-28 05:30:30
$ awk '$0 == "" {}
       $1 == "line" {line = $2}
       {print %0, line}' infile >outfile
$ awk '$0 == "" {}
       $1 == "line" {line = $2}
       {print %0, line}' infile >outfile
無處可尋 2024-10-28 05:30:30

Sed 可能不是完成这项工作的最佳工具,但

sed '/^$/d;/line/{s/line //;h;d;};G;s|\n||' filename

请注意,除了一行数字之外,所有数字行都以空格结尾;该解决方案假设这是一个拼写错误,并且他们都应该这样做。如果没有人应该,那么使用这个:

sed '/^$/d;/line/{s/line //;h;d;};G;s|\n| |' filename

如果有些可能,有些可能不,那么请谨慎行事:

sed '/^$/d;/line/{s/line //;h;d;};G;s| *\n| |' filename

Sed probably isn't the best tool for this job, but

sed '/^$/d;/line/{s/line //;h;d;};G;s|\n||' filename

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:

sed '/^$/d;/line/{s/line //;h;d;};G;s|\n| |' filename

and if some might and some might not, then play it safe:

sed '/^$/d;/line/{s/line //;h;d;};G;s| *\n| |' filename
假面具 2024-10-28 05:30:30
awk '/line/{n=$2;next}NF{$0=$0 FS n;print $0}' file

ruby -ane 'n=$F[1] if /line/; print $_.chomp + " #{n}\n" if $F.size>0 && !/line/' file
awk '/line/{n=$2;next}NF{$0=$0 FS n;print $0}' file

ruby -ane 'n=$F[1] if /line/; print $_.chomp + " #{n}\n" if $F.size>0 && !/line/' file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文