shim_mang
shim_mang

Reputation: 361

Get the difference of columns in 2 files and print it in the 2nd file in awk

I am trying to compare the values in 2 columns of 2 separate file and add the difference onto 2nd file in the last column like below:

File 2 column 5 - File 1 column 5 --> store in column 6 of File 2

File 1:

test1 data2 34 2323 433 3.32
test2 data3 32 232  32  54.54
test3 data4 34 23   76  9.43
test4 data4 32 21   12  3.777

File 2:

test1 data2 34 2323 433 4.342
test2 data3 32 232  32  22.11
test3 data4 34 23   76  8.982
test4 data4 32 21   12  7.545

Resultant File 2:
test1 data2 34 2323 433 4.342 1.022
test2 data3 32 232  32  22.11  -32.43
test3 data4 34 23   76  8.982 -0.448
test4 data4 32 21   12  7.545 3.768

I am new to awk and tried this, but this doesn't work. Can someone help me explain what's going on?

awk '{a=$5;getline<f;$5-=a;}1' f=file1.log file2.log

Upvotes: 0

Views: 61

Answers (3)

shim_mang
shim_mang

Reputation: 361

Tried this one out and seems its working:

awk '{a=$5;getline<f;$6=($5-a)/$5;}1' f=file1.log file2.log | tee file3.log

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203522

$ paste -d'\n' file1 file2 | awk '!(NR%2){print $0, $6-p} {p=$6}'
test1 data2 34 2323 433 4.342 1.022
test2 data3 32 232  32  22.11 -32.43
test3 data4 34 23   76  8.982 -0.448
test4 data4 32 21   12  7.545 3.768

Upvotes: 1

Andre Wildberg
Andre Wildberg

Reputation: 19088

This uses the unique column 1 to separate the entries within an "associative" array and doesn't rely on sorted input.

Note: using column 6 instead of 5.

% awk 'NR==FNR{value[$1]=$6; next} 
              {print $0"\t"$6 - value[$1]}' file1.log file2.log
test1 data2 34 2323 433 3.32    1.022
test2 data3 32 232  32  54.54   -32.43
test3 data4 34 23   76  9.43    -0.448
test4 data4 32 21   12  3.777   3.768

Upvotes: 3

Related Questions