tags:

views:

130

answers:

5

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)

and I need the number in the parenthesis:

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

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
Jefromi
+5  A: 
$ ..<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
ghostdog74
boom! its bomb.
ben Rod
+1  A: 

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.

martin clayton
+1  A: 

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
glenn jackman
little minor nitpick , put `-r` with read
ghostdog74
+1  A: 
sed 's/.*strip\(.*\).out.*(\([^:].*\))/\1 \2/' file