Rashedul Islam Rashedul Islam - 4 months ago 12
Linux Question

Calculate sum of every pair of numbers in columns in linux

I am looking for a Linux command that will sum every two rows in column 3 and 4. It will discard even numbered rows too. Thanks in advance.

Input:

1 10469 + 1 2
1 10470 - 1 0
1 10471 + 0 2
1 10472 - 4 0
1 10484 + 5 2
1 10485 - 0 4


Output:

1 10469 + 2 2
1 10471 + 4 2
1 10484 + 5 6

jas jas
Answer
$ awk 'NR % 2 == 1 { a=$1 OFS $2 OFS $3; b=$4; c=$5 } NR % 2 == 0 { print a, b + $4, c + $5 }' input
1 10469 + 2 2
1 10471 + 4 2
1 10484 + 5 6