So my input file is:
1;a;b;2;c;d;3;e;f;4;g;h;5 1;a;b;2;c;d;9;e;f;101;g;h;9 3;a;b;1;c;d;3;e;f;10;g;h;5
I want to sum the numbers then write it to a file (so i need every 4th
field).
I tried many sum examples on the net but i didnt found answer for my problem.
My ouput file should looks:
159
Thanks!
Update:
a;b;**2**;c;d;g 3;e;**3**;s;g;k h;5;**2**;d;d;l
The problem is the same.
I want to sum the 3th numbers (But in the line it is 3th).
So 2+3+2
.
Output: 7
Advertisement
Answer
Apparently you want to print every 3rd field, not every 4th. The following code loops through all fields, suming each one in a 3k+1
position.
$ awk -F";" '{for (i=1; i<=NF; i+=3) sum+=$i} END{print sum}' file 159
The value is printed after processing the whole file, in the END {}
block.