Wednesday, 19 March 2014

Grep command to compare the difference between 2 files.

Recently, I was dealing with a shell script. I have been using "sdiff" to find the difference between two files for a long time.

But, I came across an unique combination of command "grep" and its switches. It was extremely helpful in building my script.

So, let see how we can utilize grep, to find the diference between two files.

I have 2 files here  manoj1.txt and manoj2.txt.

Contents of file "manoj1.txt"  is displayed below:
====
root@manoj [~]# cat manoj1.txt
apple
orange
mango
grapes
====

Contents offline "manoj2.txt" is displayed:
===
root@newserver [~]# cat manoj2.txt
apple
watermelon
pineapple
jackfruit
====

Now, I am going to show the magic of grep command along with its switch "-Fxvf".

====
root@newserver [~]# grep -Fxvf manoj1.txt manoj2.txt
watermelon
pineapple
jackfruit
=====


It exactly gave me the difference.

So, to conclude with a Syntax:
 =====
 grep -Fxvf file1.txt file2.txt
====

No comments:

Post a comment

Note: only a member of this blog may post a comment.