Steg Verner
Steg Verner

Reputation: 913

Compare two files using linux commands

I want to compare two unordered but similar data files. Is it possible to do so in linux. Is yes the how?

For example the contents of my first file are:

a=5 b=10 c=78
a=8 b=11 c=75
a=8 b=11 c=54

And the contents of my second file are:

a=8 b=11 c=75
a=5 b=10 c=78

Now want to find lines which are there in File 1 but are not there in File 2 or vice versa. That is, I want to find the difference between two files. Is it possible to do so using linux commands like grep, etc. Here the difference between two files is: a=8 b=11 c=54

Upvotes: 1

Views: 149

Answers (3)

PURVESH PATEL
PURVESH PATEL

Reputation: 209

If you have vim installed, try:

vim -f filename1 filename2

This will show you difference between two files in GUI form (Visualize).

Upvotes: 0

Steephen
Steephen

Reputation: 15824

First sort your file as mentioned by myaut and use sdiff, it will show both files in 2 pan in screen and lines with difference will be marked with characher | in the dividing space in between the 2 pans.

cat file1 | sort > file1_sorted
cat file2 | sort > file2_sorted

sdiff file1_sorted file2_sorted | more

Upvotes: 0

myaut
myaut

Reputation: 11504

Use diff!

First, sort both files:

sort a > a.sorted
sort b > b.sorted

Then apply diff to them:

myaut@panther:/tmp/files> diff -u a.sorted b.sorted 
--- a.sorted    2015-02-28 20:01:46.066305657 +0300
+++ b.sorted    2015-02-28 20:01:49.299210198 +0300
@@ -1,3 +1,2 @@
a=5 b=10 c=78
a=8 b=11 c=75
-a=9 b=12 c=54

Each line in diff output may be preceded with - of + sign. In this example:

  • - means that marked line missing from file b, but exists in a.
  • + will be shown if line exists in b but is not present in a.

Upvotes: 3

Related Questions