1748154 Members
3650 Online
108758 Solutions
New Discussion юеВ

Comparing two files

 
SOLVED
Go to solution
Vidhya B
Frequent Advisor

Comparing two files

I need to compare to files and send the difference to another file.

Example:
file1:
this is the example.
I am working in hpux.

file2:
How to compare files?
this is the example.

If i compare file1 with file2, I should get the output as follows:

Output File:
I am working in hpux.

Please anyone help me with this.
12 REPLIES 12
Ismail Azad
Esteemed Contributor

Re: Comparing two files

Hi,

Use diff command.

diff file1 file2.

Welcome to ITRC.

Regards
Ismail Azad
Read, read and read... Then read again until you read "between the lines".....
Hakki Aydin Ucar
Honored Contributor

Re: Comparing two files

also you can find xdiff - X11/Motif based file comparator and merge tool.
http://reality.sgiweb.org/rudy/xdiff/
James R. Ferguson
Acclaimed Contributor

Re: Comparing two files

Hi:

Depending on what you want to do, not only 'diff' but also 'comm' and 'cmp' can be used. Read the manpages for each.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor
Solution

Re: Comparing two files

>I should get the output as follows:

What about this line: How to compare files?

Did you want all of the lines in file1 not in file2?
"comm -23 file1 file2" can do this, provided you sort the two files first.
Vidhya B
Frequent Advisor

Re: Comparing two files

Hi,

Thankyou for your replies.

Both of my files have IP Addresses.
I wanted the IP address which present in file1 but not in file2 to be moved to another file. I have tried your suggestions. I didn't exactly get what I wanted.

Please help me with this.
Dennis Handly
Acclaimed Contributor

Re: Comparing two files

>I wanted the IP address which present in file1 but not in file2 to be moved to another file.

As I mentioned:
sort file1 > file1.sort
sort file2 > file2.sort
comm -23 file1.sort file2.sort > another-file
rm -f file1.sort file2.sort
Vidhya B
Frequent Advisor

Re: Comparing two files

Thanks Dennis.

But since they are IP Addresses, that command is of no use.

I tried like that already :(

Anyways thanks a lot :)
James R. Ferguson
Acclaimed Contributor

Re: Comparing two files

Hi:

> But since they are IP Addresses, that command is of no use.

That's not true, you just have to be sort with multiple keys using the '.' as a delimiter:

# sort -t"." -k1n,1 -k2n,2 -k3n,3 -k4n,4 file.in > file.out

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Comparing two files

>But since they are IP Addresses, that command is of no use.

Please explain why sort(1) won't work? IP addresses are just strings. If there are problems because two IP strings are in the same equivalence class, that's another issue that's possible to solve.

Better yet, please provide an example of your two files that you want to process.