Using unix shell script, how to compare two files and print lines with mismatch? Below are the requirements:
The number of lines on the two files is not the same.
The difference/mismatch can be found on the second or third column.
The comparison is not between line 1 of file 1 and line 1 of file 2. Rather, the comparison is on line 1 of file 1 and the line on file 2 that has the same first word on the line 1 of file 1.
awk '{ if (FNR==NR) {arr[$1]=$0;next}
if (($1 in arr) && ($0!=arr[$1])) { f1[$1]=arr[$1]; f2[$1]=$0; next} }
END { print "from file 1"; for (i in f1) {print f1}; print "from file 2"; for (i in f2) {print f2} } ' file1 file2 > file3
Assumption: First word is unique in a given file. please let me know if you need to handle duplicates also so that I can try for that.
hi king! the code you gave me doesn't work..
it doesn't print the lines with difference though i am sure that there are lines that have mismatch on the two files.
Did you check content in file3 because I redirected output to file 3?
I tested the code with the inputs you have given and I could match your output also..
If you want the result to be printed on the screen then remove the "> file3" part from the code and run it..
Nope i was looking in same format as before..... the one using sdiff code worked fine except for records which are missing...
And its not file1 against file2 or vice verse.. Have to compare both files.. See example belore again:
File1
i just want to ask fo rhelp regarding this... the requirements is the same as the original problem stated. the only difference is that that the comparison is only on 2nd and third columns.