Skip to content
Advertisement

awk sum every 4th number – field

So my input file is:

JavaScript

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:

JavaScript

Thanks!

Update:

JavaScript

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.

JavaScript

The value is printed after processing the whole file, in the END {} block.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement