Skip to main content
3 of 3
edited tags
terdon
  • 252.7k
  • 69
  • 481
  • 719

Sum of alternate values in a column using either sed or nawk

foo.txt:

1  10     11
2   20     22
3   30     32
4   40     42
5   50     52
6   60     62
7   70     72
8   80     82
9   90     92
10  100   110

Desired Out.txt :

1  10     11
2   20     22
3   30     32
4   40     42
5   50     52
6   60     62
7   70     72
8   80     82
9   90     92
10  100   110
25  250   275   #Line 11
30  300   330   #Line 12
45  550   595  #Line 13

Line 11 is a sum of alternate lines starting from line 1 in 1st,2nd and 3rd columns, Line 12 is sum of alternate lines starting from line 2 in 1st, 2nd and 3rd columns. Line 13 is sum of columns in line 11 and line 12. I am using KSH and Solaris 5.10, The values in the input file may not be sequential and will not be more than 3 digit integers. My input file will only have 10 lines. How to achieve this?

ayrton_senna
  • 1.1k
  • 3
  • 16
  • 33