Operating System - HP-UX
1834007 Members
1804 Online
110063 Solutions
New Discussion

Re: Compare 2 files and remove duplicate lines

 
SOLVED
Go to solution
Anand_30
Regular Advisor

Compare 2 files and remove duplicate lines

HI,

I have 2 files a & b. I need to compare these 2 files and remove all the entry from file b which is same as that of file a.

Can anyone please help me do this.

Thanks
Anand
3 REPLIES 3
Sundar_7
Honored Contributor
Solution

Re: Compare 2 files and remove duplicate lines

grep -vf a b > c
mv c b
Learn What to do ,How to do and more importantly When to do ?
Hein van den Heuvel
Honored Contributor

Re: Compare 2 files and remove duplicate lines

Are those files sorted?
If they are, simply use: comm -2 a b > c
If they are not you may consider pre-sorting.

Hein.
Muthukumar_5
Honored Contributor

Re: Compare 2 files and remove duplicate lines

we can do this with diff command too as,

echo "`diff -u file1 file2 | grep -v '^++' | grep '^+'| sed -e 's/^+//'` > file2


We can use commands donot use temporary files then, we can use echo and redirection there.

sundar reply can be done as,

echo "`grep -vf file1 file2`" > file2

No temporary files there.
Easy to suggest when don't know about the problem!