0

I need to sum the the numbers located in the same column in an array.

The input is:

1: 6.1703
44 3.5 0.3
46 7.3 2.3

2: 6.1932
44 5.29379 2.9
46 1.72261 7.2261

3: 6.5664
45 4.756 5.6
46 1.6 2.5

4: 8.0923
44 1.41 7.6

The output should be:

1: 6.1703
 10.8 2.6

2: 6.1932
 7.01640 10.1261

3: 6.5664
 6.356 8.1

4: 8.0923
 1.41 7.6
  • the input comes from a text file or a user ? – onurcanbektas Jun 6 '16 at 11:37
0

A slightly different approach using awk -v RS="" -f script data, where script is the following:

{
    s1 = s2 = 0;
    for (i=4; i < NF; i = i + 3) {
        s1 += $i;
        s2 += $(i+1);
    }
    print $1, $2 "\n " s1, s2 "\n";
}

This makes use of the fact that the "arrays" are separated by blank lines.

0

try

/:/ { if ( NR>1 ) printit() ; print ; two=0 ; three=0 ; next;}
NF > 2 { two+=$2 ; three+=$3 ; next }
{ print ; }
function printit() {  printf " %s %s\n",two,three; }
END { printit() ;}
  • function printit print actual result
  • sums are computed on a line with a colon (:).
  • END clause is used for final printing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.