0

Let say I have file1 with the following content:

1,2,3,4,12,6
1,2,3,5,333,6
1,2,3,12,5,6
1,2,3,44,101,6

While file2 has the following content:

22,4,111,1,6,7,12
22,4,56,55,6,7,12
22,4,12,12,6,7,12
22,4,333,102,6,7,12

What I want is if 5th field on file1 has a greater value than the 3rd field on file2 print something like Value 333 greater than 56

How could this be done using awk?

1
$ awk -F, 'FILENAME=="file1" {inputs[FNR]=$5} FILENAME=="file2" && inputs[FNR] > $3 {print inputs[FNR] " is greater than " $3 " on line " FNR}' file1 file2
333 is greater than 56 on line 2
  • Great! @DopeGhoti. Could you please explain what is going on? Why do you use e.g FILENAME=="file2" && inputs[FNR] here the && and by the first FILENAME=="file1" you are not. Thanks! It works – holasz Dec 20 '18 at 21:00
  • The code populates an array only when reading file1. It makes the comparison only when reading file2 and only prints the output when the comparison matches the specified criterium. – DopeGhoti Dec 20 '18 at 21:02
0

I have used below method to get the same required output

for i in {1..4}; do second_file=`awk -F "," -v i="$i" 'NR==i{print $3}' j.txt`; first_file=`awk -F "," -v i="$i" 'NR==i {print $5}' l.txt`; if [[ $first_file -gt  $second_file ]]; then echo $first_file is greater than $second_file; fi; done

output

333 is greater than 56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.