cgol
May 25, 2011, 4:22pm
1
Hi all,
Looking for suggestions on a better way to sum numbers in a key value pair formated file. What I have works but seems really clunky to me. Any suggestions would be greatly appreciated.
cat test.txt | perl -ne 'm/(M=)(\d+\.?\d?\d?)/ && print "$2\n"' | awk '{ sum+=$1} END {printf "%18.2f\n",sum}'
Sample data (test.txt):
123 { id=111 M=12. ST=OR}
123 { id=112 M=12.0 ST=ID}
123 { id=113 M=12.22 ST=NM}
123 { id=114 M=12.00 ST=NY}
123 { id=115 M=12. ST=WA}
123 { id=116 M=12 ST=IA}
123 { id=117 M=22.54 ST=MA}
And what is the desired output? Also explain in more detail how you want this sum to be calculated (which field, when etc).
nawk -F'[= ]' '{s+=$6}END {printf("%18.2f\n", s)}' test.txt
cgol
May 25, 2011, 4:39pm
4
Sorry forgot to mention that the each line in the file can vary:
123 { id=111 X=123 M=12. ST=OR}
123 { id=112 X=123 y=123 M=12.0 ST=ID}
123 { id=113 Z=222 M=12.22 ST=NM}
123 { id=114 X=321 Y=1 Z=88 M=12.00 ST=NY}
123 { id=115 X=1 M=12. ST=WA}
123 { id=116 X=222 M=12 ST=IA}
123 { id=117 M=22.54 ST=MA}
I need it formatted in as 18.2 and need the M value included in the summary for each row as it will never be missing. Thanks again.
nawk -F'[= ]' '{s+=$(NF-2)}END {printf("M=%18.2f\n", s)}' test.txt
OK, please provide the desired output based on your sample file - this is getting vague
cgol
May 25, 2011, 5:07pm
6
Sorry for being vague...was simply looking for the sum to be output with two decimal points so nothing fancy. This works great, thanks for your help. How would you implement this if the position of M wasn't the same going right to left?
123 { id=110 X=123 M=12. ST=OR XX=2}
123 { id=111 M=12. ST=OR}
123 { id=112 X=123 y=123 M=12.0 ST=ID R=4}
123 { id=113 Z=222 M=12.22 ST=NM QQ=2 L=3}
It can be done in a single Perl line:
perl -ne '$s+=(/(?<=M=)[\d.]+/g)[0];END{printf "%18.2f\n",$s}' test.txt
cgol
May 25, 2011, 5:47pm
8
Nice, the perl version is very fast too. Thanks for all the tips.