2013-02-06 56 views
-1

我有3個文件:AWK比較兩個文件,找到最小值/最大值,並將其存儲

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   -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
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