AWK比较两个文件,​​找到最小值/最大值并存储 [英] awk compare 2 files, find min/max and store it

查看:112
本文介绍了AWK比较两个文件,​​找到最小值/最大值并存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个文件:

File_1是静态的,内容不发生变化,瓦莱斯可以是-160至0:


xdslcmd:ADSL驱动器和PHY状态
状态:空闲
重新调校原因:0色调数QLN
   0 0.0000
   1 0.0000
   2 0.0000
   3 0.0000
   4 0.0000
   5 0.0000
   6 0.0000
   7 -160.0000
   8 -119.2000
   9 -128.6700
   10 -113.1200
   11 -93.1000
   12 -130.0000
   13 -120.0000
   14 -110.0000
   15 -100.0000
   16 -90.0000
   17 -100.0000
   18 -110.0000
   19 -120.0000
   20 -130.0000
   21 -140.0000
   22 -110.0000
   23 0.0000
   24 0.0000

File_2是看起来像File_1但观念正在转变每次(值可以在-160至0)


xdslcmd:ADSL驱动器和PHY状态
状态:空闲
重新调校原因:0色调数QLN
   0 0.0000
   1 0.0000
   2 0.0000
   3 0.0000
   4 0.0000
   5 0.0000
   6 0.0000
   7 -160.0000
   8 -159.2000
   9 -148.6700
   10 -123.1200
   11 -83.1000
   12 -100.0000
   13 -100.0000
   14 -100.0000
   15 -80.0000
   16 -80.0000
   17 -110.0000
   18 -120.0000
   19 -130.0000
   20 -140.0000
   21 -150.0000
   22 -100.0000
   23 0.0000
   24 0.0000

我想比较File_2 $ 2 File_1 $ 2和存储它们之间的区别INF File_3

〔实施例:


File_1包含:18 -120.0000
File_2包含:18 -140.0000
预计产量:18 -20 0

File_1包含基本值(认为是0)
File_2的每一次变化,持有的实际值。
期望的输出是在测量过程中的最小值/最大值从基值差

有可能的是在相同的色调QLN可以在测量过程中是较高和较低的:


File_1包含:18 -120.0000
File_2包含:18 -140.0000
File_2包含:18 -100.0000(在后面的查询)
预计产量:18 -20 +20

File_1和File_2进行排序,第一个5行是不相关的。


解决方案

 的awk'FNR 6; {下一} NR == FNR {a [$ 1] = $ 2;接下来} {printf的%S \\ T%10F \\ n,$ 1,$ 2-A [$ 1]}'F1 F2
0 0.000000
1 0.000000
2 0.000000
3 0.000000
4 0.000000
5 0.000000
6 0.000000
7 0.000000
8 -40.000000
9 -20.000000
10 -10.000000
11 10.000000
12 30.000000
13 20.000000
14 10.000000
15 20.000000
16 10.000000
17 -10.000000
18 -10.000000
19 -10.000000
20 -10.000000
21 -10.000000
22 10.000000
23 0.000000
24 0.000000

非零差异:

 的awk'FNR 6; {下一} NR == FNR {a [$ 1] = $ 2;接下来} D = $ 2-A [$ 1] {printf的%S \\ T% 10F \\ n,$ 1,D}F1 F2
8 -40.000000
9 -20.000000
10 -10.000000
11 10.000000
12 30.000000
13 20.000000
14 10.000000
15 20.000000
16 10.000000
17 -10.000000
18 -10.000000
19 -10.000000
20 -10.000000
21 -10.000000
22 10.000000

使用的printf 意味着你可以改变输出的格式,例如只有小数点后两位的printf%S \\ T% 10.2f \\ n,$ 1,D

I have 3 files:

File_1 is static, the content is not changing, vales can be -160 to 0:

xdslcmd: ADSL driver and PHY status
Status: Idle
Retrain Reason: 0

Tone number      QLN
   0            0.0000
   1            0.0000
   2            0.0000
   3            0.0000
   4            0.0000
   5            0.0000
   6            0.0000
   7            -160.0000
   8            -119.2000
   9            -128.6700
   10           -113.1200
   11           -93.1000
   12           -130.0000
   13           -120.0000
   14           -110.0000
   15           -100.0000
   16           -90.0000
   17           -100.0000
   18           -110.0000
   19           -120.0000
   20           -130.0000
   21           -140.0000
   22           -110.0000
   23           0.0000
   24           0.0000

File_2 is looks like File_1 but the values are changing every time (values can be -160 to 0)

xdslcmd: ADSL driver and PHY status
Status: Idle
Retrain Reason: 0

Tone number      QLN
   0            0.0000
   1            0.0000
   2            0.0000
   3            0.0000
   4            0.0000
   5            0.0000
   6            0.0000
   7            -160.0000
   8            -159.2000
   9            -148.6700
   10           -123.1200
   11           -83.1000
   12           -100.0000
   13           -100.0000
   14           -100.0000
   15           -80.0000
   16           -80.0000
   17           -110.0000
   18           -120.0000
   19           -130.0000
   20           -140.0000
   21           -150.0000
   22           -100.0000
   23           0.0000
   24           0.0000

I want to compare File_2 $2 to File_1 $2 and store the difference between them inf File_3

Exmaple:

File_1 contains:    18           -120.0000
File_2 contains:    18           -140.0000
Expected output:    18           -20            0

File_1 contains the base values (considered as "0") File_2 changes every time and holds the actual values. The expected output is the min/max difference from the base values during the measurement.

It is possible that in the same tone QLN can be higher and lower during the measurement:

File_1 contains:    18           -120.0000
File_2 contains:    18           -140.0000
File_2 contains:    18           -100.0000   (in a later query)
Expected output:    18           -20            +20

File_1 and File_2 are sorted, the first 5 lines are not relevant.

解决方案

awk 'FNR<6{next}NR==FNR{a[$1]=$2;next}{printf "%s\t%10f\n",$1,$2-a[$1]}' f1 f2
0     0.000000
1     0.000000
2     0.000000
3     0.000000
4     0.000000
5     0.000000
6     0.000000
7     0.000000
8   -40.000000
9   -20.000000
10  -10.000000
11   10.000000
12   30.000000
13   20.000000
14   10.000000
15   20.000000
16   10.000000
17  -10.000000
18  -10.000000
19  -10.000000
20  -10.000000
21  -10.000000
22   10.000000
23    0.000000
24    0.000000

Non-zero differences:

awk 'FNR<6{next}NR==FNR{a[$1]=$2;next}d=$2-a[$1]{printf "%s\t%10f\n",$1,d}' f1 f2
8   -40.000000
9   -20.000000
10  -10.000000
11   10.000000
12   30.000000
13   20.000000
14   10.000000
15   20.000000
16   10.000000
17  -10.000000
18  -10.000000
19  -10.000000
20  -10.000000
21  -10.000000
22   10.000000

The use of printf means you can change the format of the output, for instance to only two decimal places printf "%s\t%10.2f\n",$1,d.

这篇关于AWK比较两个文件,​​找到最小值/最大值并存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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