f_o_555
December 17, 2009, 2:33am
1
Hi All,
I have like this
M17XX-050-01 0100000000 QQSSS
0.0000e+00 1.712E+06 1.255E+07 0.0000e+00
0.0000e+00 1.722E+06 1.263E+07 0.0000e+00
...
0.0000e+00 1.204E+06 8.829E+06 0.0000e+00
M17XX-050-01 0100000000 WWSSS
0.0000e+00 7.564E+03 1.165E+01 0.0000e+00
0.0000e+00 8.008E+03 1.233E+01 0.0000e+00
...
0.0000e+00 7.286E+03 1.122E+01 0.0000e+00
0.0000e+00 7.286E+03 1.122E+01 0.0000e+00
...
So a description line, N lines with 4 columns, description line, N lines with 4 columns and so on.
I would like to have a file where I have in the first column the sum of the other three columns.
Do you have an efficient solution to solve the problem?
Thank you in advance,
Sarah
awk '{if (NF==4) print $2+$3+$4 ;else print }' filename
ichigo
December 17, 2009, 2:44am
3
gawk '!/^M/{ $0=$2+$3+$4 }{print} END{ print "show desired output next time" } ' file
f_o_555
December 17, 2009, 2:52am
4
The first one gives something like (the other gets stuck)
M17XX-050-01 0100000000 AAAAA
0.0000e+00 927158
0.0000e+00 846126
...
0.0000e+00 413359
M17XX-050-01 0100000000 BBBBB
0.0000e+00 14262000
0.0000e+00 14352000
...
0.0000e+00 10033000
This should be the output
M17XX-050-01 0100000000 AAAAA
9.2716E+05 9.2680E+05 3.5750E+02 0.0000E+00
8.4613E+05 8.4580E+05 3.2620E+02 0.0000E+00
...
4.1336E+05 4.1320E+05 1.5940E+02 0.0000E+00
M17XX-050-01 0100000000 BBBBB
1.4262E+07 1.7120E+06 1.2550E+07 0.0000E+00
1.4352E+07 1.7220E+06 1.2630E+07 0.0000E+00
...
1.0033E+07 1.2040E+06 8.8290E+06 0.0000E+00
awk -F'\t' '
(/^[0-9]/ && $1 = sprintf("%.4E", $2 + $3 + $4)) || -3
' OFS='\t' infile
Use gawk if available, otherwise use nawk or /usr/xpg4/bin/awk.
Do not use /usr/bin/awk on Solaris.
f_o_555
December 17, 2009, 4:27am
6
Thank you,
does it carry out the sum only when the first coulmn is 0 or in any case?
I would need to to the sum only if the first column is 0.
No,
try this for the new requirement:
awk -F'\t' '
(/^[0-9]/ && $1 == 0 && $1 = sprintf("%.4E",$2 + $3 + $4)) || -3
' OFS='\t' infile