I have a file which looks like:
1 4
2 4
3 5
4 4
5 4
6 1
7 1
8 1
9 4
10 4
12 1
13 1
14 1
15 1
16 2
19 3
20 1
21 1
26 1
28 3
24 4
29 4
30 1
The column 1 is serial number and column2 is the values. I would like to calculate the sum of values between a particular range for Eg: sum the values in column2 which are between 2 and 7 (from column1)
I acheived this by the following awk one liner:
awk '{if ($1 >= 2 && $1 <= 7) x += $2 } END {print x}' file_name #output is 20
The question is I would like to read the ranges from other file2: From 3-9, 2-6, 12-20 etc
3 9
2 6
12 20
How could I pass the range from file2 to the AWK instead of manually typing the range with if statement. How to read multiple files in AWK ?