开发者

How do I extract the values from this data using bash and awk?

I grepped these, how do I extract the values?

...
cavity_2mgl_wt_strip57001.out: Total cavity volume (A3)               : (  1.240E+01) 
cavity_2mgl_wt_strip58001.out: Total cavity volume (A3)               : (  2.408E+00) 
cavity_2mgl_wt_strip60001.out: Total cavity volume (A3)               : (  4.935E+00) 
cavity_2mgl_wt_strip61001.out: Total cavity volume (A3)               : (  1.319E+00) 
cavity_2mgl_wt_strip63001.out: Total cavity volume (A3)               : (  1.532E-01) 
cavity_2mgl_wt_strip64001.out: Total cavity volume (A3)               : (  1.137E+01) 
...

and I need the index # in the filename in bold:

cavity_2mgl_wt_strip76001.out: Total cavity volume (A3)               : (  1.276E+01)开发者_JAVA百科

and I need the number in the parenthesis:

cavity_2mgl_wt_strip76001.out: Total cavity volume (A3)               : (  1.276E+01)


$ ..<commands>.. | awk -F"[:)(]" '{gsub(".*strip|.out","",$1);print $1,$(NF-1)}' 
57001   1.240E+01
58001   2.408E+00
60001   4.935E+00
61001   1.319E+00
63001   1.532E-01
64001   1.137E+01

or if your grepped values are already in a file

$ awk -F"[:)(]" '{gsub(".*strip|.out","",$1);print $1,$(NF-1)}' file


sed 's/.*strip\(.*\).out.*(\([^:].*\))/\1 \2/' file


Sure is a lot shorter in perl than awk. I don't know how flexible your format is; I put in a few wildcards just in case:

perl -ne 'if ($_ =~ /cavity_[0-9]+mgl_wt_strip([0-9]+)\.out:[^:]+: *\( *([0-9]+\.[0-9]+E[+-][0-9]+)\)/) {print "$1 $2\n"}' in.txt > out.txt


How about using sed?

sed -e 's/.*strip\([^.]*\).*( *\([^ )]*\) *).*/\1 \2/' infile > outfile

The first capture is of the part of the line between "strip" and the next dot. Then the line until the last opening bracket is skipped. The second capture is of the number (with any leading and trailing space removed) between the last pair of brackets.


In pure bash:

while read line; do
   tmp="${line#*strip}"; index="${tmp%.out*}"
   tmp="${line##*(}";    value="${tmp%)*}"
   printf "%s:%s\n" "$index" "$value"
done < file
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜