0

The following lines sum numbers located in the row #2 and row #3 , row #4 and row #5, row #6 and row #7 and so on (so, sum in a consecutive way).

awk ' NR==1{$1=$1; print; next} !(NR%2){split($0,a);print; next} {for(i=1;i<=NF;i++) $i+=a[i]}1' file

Well , I am trying to "generalize" the previous code, so I need to do something to sum whatever rows, for example what I need to do to obtain a sum like this:

  row #2 + row #3 + row #4 + ... + row #10 + row #14 + row #15 + row #19
  row #1 + row #11 + row #12 + row #13 + row #16 + ... +row #18 + row #20
0

If you want to specify the relevant column numbers by hand and then just use awk to print out two sums at the end, you can do:

awk 'NR == 2 || NR == 3 || ... {sum1 += $1}
     NR == 1 || NR == 11 || ... {sum2 += $1}
     END {print sum1; print sum2}' file

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.