Simon Simon - 2 months ago 8
Bash Question

bash : check if sum of specific words smaller than given number

I have 300 files consisting of this format :

0 0 261157 0 13267 0 314 0 274738 736485 999.999756
1 0 261155 0 13269 0 314 0 274738 736625 1000.147705
2 0 261162 0 13264 0 312 0 274738 736703 1000.104370
3 0 261156 0 13265 0 312 0 274733 736838 1000.113708
4 0 261166 0 13261 0 311 0 274738 736918 999.999756
5 0 261173 0 13258 0 311 0 274742 737054 1000.001892
6 0 261153 0 13284 0 311 0 274748 737137 1000.204529
7 0 261152 0 13280 0 312 0 274744 736800 1000.188110
8 0 261154 0 13277 0 311 0 274742 737081 1000.107178
9 0 261150 0 13278 0 310 0 274738 737309 1000.189392
10 2348 260942 0 13271 0 310 2348 274523 737683 1001.341980
11 2342 260915 0 13264 0 310 2342 274489 737720 1000.341797
12 2340 260900 0 13272 0 310 2340 274482 738490 1000.266357
13 2339 260885 0 13284 0 309 2339 274478 738363 1000.312317


This is an example of a section of my files. The number of lines can range from 1000 to 50000 depending on the file.

Right now, I need to check the second column and second last column of the last line. If the sum of these numbers is smaller than 10, this implies my data has something wrong.

I know how to print out the last line by "tail", but I have never tried to extract the numbers, sum them up and compare to a number to see if it is smaller or greater.

Thanks.

PS. PS.
Answer
awk 'END{x=($2+$(NF-1));if(x<10) print "Wrong";else print "correct"}' test
correct

Sample data used :

cat test

0       0       261157  0       13267   0       314     0       274738  736485  999.999756
1       0       261155  0       13269   0       314     0       274738  736625  1000.147705
2       0       261162  0       13264   0       312     0       274738  736703  1000.104370
3       0       261156  0       13265   0       312     0       274733  736838  1000.113708
4       0       261166  0       13261   0       311     0       274738  736918  999.999756
5       0       261173  0       13258   0       311     0       274742  737054  1000.001892
6       0       261153  0       13284   0       311     0       274748  737137  1000.204529
7       0       261152  0       13280   0       312     0       274744  736800  1000.188110
8       0       261154  0       13277   0       311     0       274742  737081  1000.107178
9       0       261150  0       13278   0       310     0       274738  737309  1000.189392
10      2348    260942  0       13271   0       310     2348    274523  737683  1001.341980
11      2342    260915  0       13264   0       310     2342    274489  737720  1000.341797
12      2340    260900  0       13272   0       310     2340    274482  738490  1000.266357
13      2339    260885  0       13284   0       309     2339    274478  738363  1000.312317