awk比较2个文件,找到min / max并存储它

时间:2013-02-06 14:17:12

标签: awk

我有3个文件:

File_1是静态的,内容不会改变,vales可以是-160到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看起来像File_1,但值每次都在变化(值可以是-160到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

我想将File_2 $ 2与File_1 $ 2进行比较并存储它们之间的差异inf file_3

〔实施例:

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

File_1包含基值(视为“0”) File_2每次都会更改并保存实际值。 预期输出是测量期间与基准值的最小/最大差异。

在测量过程中,QLN可能会在同一音调中更高和更低:

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和File_2进行排序,前5行不相关。

1 个答案:

答案 0 :(得分:1)

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

非零差异:

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

printf的使用意味着你可以改变输出的格式,例如只改为两个小数位printf "%s\t%10.2f\n",$1,d