Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

awk compare 2 files, find min/max and store it

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
awk 'FNR<6{next}NR==FNR{a[$1]=$2;next}{printf "%s	%10f
",$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	%10f
",$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 %10.2f ",$1,d.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...