开发者

Modifying a Textfile using Regular Expressions (awk?)

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开发者_JAVA技巧 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


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


In Perl you can do:

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

Ideone Link


$ awk '$0 == "" {}
       $1 == "line" {line = $2}
       {print %0, line}' infile >outfile


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜