Compare two files and output the differences
Solution 1
If you don't care about the line order, sort the files first. To see what lines are missing in what file, use comm
instead of diff
:
comm <(sort file1) <(sort file2)
Solution 2
Objective 1: Find what is missing in file2.txt from file1.txt
With grep
:
grep -xvFf file2.txt file1.txt
With comm
:
comm -13 <(sort file1.txt) <(sort file2.txt)
With sort
and uniq
:
sort file2.txt file2.txt file1.txt | uniq -u
Objective 2: Find what is missing in either file. Some lines may exist in file2.txt that arent in file1.txt. I'd like to know about them as well.
With grep
:
grep -xvFf file1.txt file2.txt; grep -xvFf file2.txt file1.txt
With comm
:
comm -3 <(sort file1.txt) <(sort file2.txt) | tr -d '\t'
With sort
and uniq
:
sort file1.txt file2.txt | uniq -u
Related videos on Youtube
unixpipe
Updated on September 18, 2022Comments
-
unixpipe over 1 year
I am aware of
diff
and using loops but I just cant seem to really get what I need with diff. I'm basically looking to compare two files (file2.txt and file2.txt) and just get the output of what is missing between them.Objective 1: Find what is missing in file2.txt from file1.txt
Objective 2: Find what is missing in either file. Some lines may exist in file2.txt that arent in file1.txt. I'd like to know about them as well.
diff
only tells me that the two files arent the same, going line by line comparing the differences. What I need is a program that goes through the file, and doesn't discriminate by lines. If a line containing '/bin/mount' is found on line 2 of file1.txt and is found on line 59 of file2.txt, then I don't need to know about it. I only want to know what isn't there as a whole. Can this be done? -
unixpipe over 9 yearsWhat a simple and easy command. Never knew about it. But how can I grep out only the unique entries from whatever column I want?
-
choroba over 9 years@unixpipe: Have you read
man comm
? -
unixpipe over 9 yearsYou guys are the best
-
unixpipe over 9 yearsI just did now, apologies. I am able to suppress either columns. These are great answers. I wish I can answer you guys both because they are both right!