Site icon New Generation Enterprise Linux

Easy way: To Find difference between two text files in Linux.

To find the differences between two files in Linux using grep, we can use the following approaches.

1. Lines in File1 but not in File2

# grep -Fxv -f file2.txt file1.txt

-F: Matches fixed strings (not patterns).
-x: Matches whole lines.
-v: Inverts the match (shows lines not found in file2.txt).
-f: Specifies the file to compare against.

This will display lines that are in file1.txt but not in file2.txt.

2. Lines in File2 but not in File1

# grep -Fxv -f file1.txt file2.txt

This works similarly but shows lines in file2.txt that are not in file1.txt.

3. Combine Both for Full Difference
To see all differences (lines unique to either file), you can combine the above commands:

# grep -Fxv -f file2.txt file1.txt
# grep -Fxv -f file1.txt file2.txt

Or, for a more compact output:

grep -Fxv -f file2.txt file1.txt; grep -Fxv -f file1.txt file2.txt;

For more advanced and detailed comparisons, we can also use diff or sdiff command instead, as it is specifically designed for this purpose.

0 0 votes
Article Rating
Exit mobile version