简体   繁体   English

bash :检查特定单词的总和是否小于给定数字

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

I have 300 files consisting of this format :我有 300 个由以下格式组成的文件:

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.行数可以从 1000 到 50000 不等,具体取决于文件。

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.如果这些数字的总和小于 10,这意味着我的数据有问题。

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.我知道如何通过“tail”打印出最后一行,但我从未尝试过提取数字,将它们相加并与数字进行比较以查看它是更小还是更大。

Thanks.谢谢。

Use tail to extract the last line and feed it to awk :使用tail提取最后一行并将其提供给awk

$ tail -n 1 file | awk '$2 + $(NF-1) < 10 {print "Wrong"}'

In case your file has lots of rows, it's faster.如果您的文件有很多行,它会更快。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM