ben Rod
ben Rod

Reputation: 31

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)

and I need the number in the parenthesis:

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

Upvotes: 2

Views: 350

Answers (5)

ghostdog74
ghostdog74

Reputation: 343141

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

Upvotes: 2

glenn jackman
glenn jackman

Reputation: 247210

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

Upvotes: 1

martin clayton
martin clayton

Reputation: 78225

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.

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 343141

$ ..<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

Upvotes: 6

Cascabel
Cascabel

Reputation: 497692

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

Upvotes: 1

Related Questions