Adding in Awk

Hello,
suppose I have a file that consists of a single column of various numbers, as in

12.010
1.0080
1.0080
0.8780
0.1350
0.0000
-0.4157
0.2719

How can I use AWK (or equivalent) to add the numbers of two specific lines? I want to sum, for example, the first with the fifth, the second with the sixth and so on.
Thanks in advance.

You could do something like:

awk '{ lines[NR]=$0 } END { print lines[1]+lines[5] }' filename
1 Like
awk 'NR < 5{a[NR%5]=$0; next}{print $0 + a[NR%5+1]}' file

Use nawk or /usr/xpg4/bin/awk on Solaris if you get errors.

1 Like

Thanks. How can I do it iteratively? Say, every line with one 4 lines ahead of it?

---------- Post updated at 03:45 PM ---------- Previous update was at 03:34 PM ----------

I hadn't refreshed and I hadn't seen the post above mine before posting...
What does [NR%5] mean?

NR is a built-in awk variable for the current record (line) number.

%5 is modulo 5 (remainder after dividing by 5, integer arithmetic).