1834939 Members
2136 Online
110071 Solutions
New Discussion

Re: Comparing two files

 
Amit Dixit_2
Regular Advisor

Comparing two files

Hi I am having two file for which I want to run a compare line by line i.e. I want to extract all lines one by one and run a find on the other file if found ignore else redirect the output to the third file.

Both the files are not sorted.


Thanks,
Amit.
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor

Re: Comparing two files

You should look at diff. Man diff for details.
If it ain't broke, I can fix that.
Denver Osborn
Honored Contributor

Re: Comparing two files

so you want to read file1 and while it's read search file2 for that same line? If the line in file1 is not found in file2, then dump it to file3? In other words, pull out the lines that don't repeat when file1 and file2 are combined...

Try uniq.

cat file1 file2 |uniq -u

hope this helps,
-denver
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Comparing two files

Have a look at the man pages of diff and cmp.

Further if you want check for common lines, you can grep

grep -f file1 file2 > file3

In this common lines will go in file3
Vibhor Kumar Agarwal
vasundhara
Frequent Advisor

Re: Comparing two files

sdiff -l file1 file2

Will give all the common lines in first line.

If a line is present in file1, but not in file2, it is represented with <

If a line is present in file2, but not in file1, it is represented with >

If a line is different in both the files, it is represented with |

man sdiff will give more information.
Bharat Katkar
Honored Contributor

Re: Comparing two files

Hi Amit,
Vibhor has got you correct.

# grep -f file1 file2 > file3

This should help.
Regards.
Bharat
You need to know a lot to actually know how little you know
john korterman
Honored Contributor

Re: Comparing two files

Hi,

# sort file1 file2|comm -3 >file3

may also work.

regards,
John K.
it would be nice if you always got a second chance