0

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
shim_mang
  • 323
  • 4
  • 17
  • Could you please explain more on logic part like how you are getting expected output? Sorry its not clear as of now. – RavinderSingh13 Feb 16 '22 at 12:23
  • 1
    When you say `File 2 column 5 - File 1 column 5 --> store in column 6 of File 2` do you actually mean `File 2 column 6 - File 1 column 6 --> store in column 7 of File 2`? If so, please [edit] your question to fix that, if not then please clarify what you do mean. – Ed Morton Feb 16 '22 at 14:37
  • If you're ever considering using `getline`, please read and make sure you fully understand http://awk.freeshell.org/AllAboutGetline first. – Ed Morton Feb 16 '22 at 14:40

3 Answers3

3

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
Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29
1
$ 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
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • Thanks, this helped. I also have another solution to it and have posted it. Please review if it is fine. The output seems ok for me. – shim_mang Feb 16 '22 at 16:42
0

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
shim_mang
  • 323
  • 4
  • 17