Help with sum range of data set together

Input File:

2000    3
1998    2
1997    2
1994    1
1991    1
1989    1
1987    2
1986    2
1985    1
1984    1
.
.
10      277256
9       278274
8       282507
7       284837
6       287066
5       292967
4       314583
3       342942
2       441704
1       487042

Desired Output

2000 110
1900 163
1800 139
.
.
.
200 3557341
100 13454579

Code Try:

awk '$1>=1901&&$1<=2000{total = total + $2}END{print total}' Input_File
110
awk '$1>=1801&&$1<=1900{total = total + $2}END{print total}' Input_File
163
awk '$1>=1701&&$1<=1800{total = total + $2}END{print total}' Input_File
139
awk '$1>=101&&$1<=200{total = total + $2}END{print total}' Input_File
3557341
awk '$1>=1&&$1<=100{total = total + $2}END{print total}' Input_File
13454579

I plan to sum the column 2 if column 1 is within every 100 group.
eg.
Sum all the column 2 value if column 1 is range within 1 to 100;
Sum all the column 2 value if column 1 is range within 101 to 200;
Sum all the column 2 value if column 1 is range within 201 to 300;

Desired output should have 20 line which column 1 is every 100 and sum its corresponding column 2 data (maximum record in data set is 2000).
The command I try is quite tedious as I need to type the command one by one from 1 to 100 till 1901 to 2000 and sum its corresponding column 2 data.

Thanks for any advice.

Hi,
Can you try:

awk '{X=sprintf("%i00",($1+99)/100);A[X]+=$2};END{for (i in A){print i,A}}' file

Regards.

1 Like

Thanks a lot and again, disedorgue.
Your awk command work perfectly.
It given the same result as I run it one by one.

Is below the command if I would like to edit become every 50 and count its corresponding column 2?

awk '{X=sprintf("%i00",($1+49)/50);A[X]+=$2};END{for (i in A){print i/2,A}}' Input_File

No, work just for power of 10 like 10,100,1000,...
General case could be (example with range 30 by 30):

awk '{X=sprintf("%1.0d",($1+29)/30);A[X*30]+=$2};END{for (i in A){print i,A}}' Input_File

or with variable:

awk -vRGE=30 '{X=sprintf("%1.0d",($1+(RGE-1))/RGE);A[X*RGE]+=$2};END{for (i in A){print i,A}}' Input_File

Regards.

1 Like

Thanks a lot and again, disedorgue.
Really appreciate for your help.

It worked perfect in my case now.
Thanks.