1
votes

Compare columns 1 and 2 for File1 and File2. Stop the comparision at the fisrt time the values does not match and exit the process

File1

1 31789.00 37145.00
1 32221.00 37145.00
1 56783.00 37145.00
1 32223.00 37145.00
1 31793.00 37145.00

File2

1 31789.00 37145.00
1 32221.00 37145.00
1 31791.00 37145.00
1 32223.00 99999.00
1 31793.00 37145.00

I tried

  awk 'NR==FNR{a[$0];next}(!($0 in a)){print}' File1 File2

I don't want to show lines which does not match, I will like to stop the process on error and say the line where the error is found

Something like that:

Error found in line 3
3

3 Answers

1
votes

Like this:

awk 'NR==FNR{a[NR]=$1;b[NR]=$2;next}
     a[FNR]!=$1||b[FNR]!=$2{print "error in line "FNR; exit 1}' file1 file2
1
votes

Could you please try following. Considering that you DO NOT want to print matching lines.

awk 'FNR==NR{a[$1,$2]=$0;next} !(($1,$2) in a){print "Error found in line "FNR;exit}'  Input_file1   Input_file2

exit in above code will help you to come out of this execution ASAP, in case you don't need it you could remove it then from above code.

Adding a non-one liner form of above solution too now.

awk '                                  ##Starting awk program here.
FNR==NR{                               ##Checking condition which will be TRUE when first Input_file(Input_file1) is being read.
  a[$1,$2]=$0                          ##Creating an array named a whose index is $1,$2 and value is $0.
  next                                 ##next will skip all further statements from here.
}                                      ##Closing first condition block here.
!(($1,$2) in a){                       ##Checking condition if $1,$2 of 2nd Input_file is NOT in array a then do following.
  print "Error found in line "FNR      ##printing error which tells ERROR not found with number of line.
  exit                                 ##exit keyword will exit from awk prpgram.
}
'  Input_file1  Input_file2            ##Mentioning Input_file names Input_file1 and Input_file2 here.
1
votes

this will be simpler

$ paste file1 file2 | awk '$1!=$4 || $2!=$5 {print "Error found in line " NR; exit 1}'
Error found in line 3