使用shell脚本或awk的最大差异 [英] Maximum of the differences using shell script or awk

查看:56
本文介绍了使用shell脚本或awk的最大差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个文件,每个文件有两列,其中包含一些错误的数据,例如较大的负值.

I have several files, each having two columns with some wrong data as large negative values.

file_1.txt

file_1.txt

3       4
4       5
2       4
-10023  -9821
4       7
9       1
3       5
3       4

file_2.txt

file_2.txt

6       2
4       5
2       4
-98323  -83432
-208932 4
7       17
20      3
20      2

file_3.txt

file_3.txt

4       4
2       4
2       4
-129923 -1209923
2       3
12      3
2       4
7       1

我想打印上述文件中第1列和第2列之间的最大差异,而不考虑错误的数据.

I would like to print the maximum of the differences between the column 1 and column 2 in the above files without considering the wrong data. In simple way,

Maximum [ ($1 - $2) file*.txt ]

所需的输出

ofile.txt
4
-1
-2
-99999
-1
9
17
18

推荐答案

下面的操作可一次通过您的示例输入(假定所有数据文件具有相同的行数):

The following works with your sample input in a single pass (Assuming all data files have the same number of lines):

$ awk '$1 > -1000 && $2 > -1000 {
         d = $1 - $2
         if (FNR in diffs) {
           if (diffs[FNR] < d)
             diffs[FNR] = d
         } else {
           diffs[FNR] = d
         }
       }
       END {
         for (n = 1; n <= FNR; n++) {
           if (n in diffs)
             print diffs[n]
           else 
             print -99999
         }
       }' file_*.txt
4
-1
-2
-99999
-1
9
17
18

这篇关于使用shell脚本或awk的最大差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆