Operating System - Linux
1753506 Members
4901 Online
108794 Solutions
New Discussion юеВ

Re: Comparing two files in hp-ux

 
vvsha
Frequent Advisor

Comparing two files in hp-ux

Hi,

I am trying to compare two files in hp-ux using diff command.

but I am not getting the proper output.

The following command I am using to find out what is in the
second file but not in the first file.

# diff file1.txt file2.txt | sed -e 's//file2content/'

Can anyone help me on this.

Is there any other command to achive the same goal?
11 REPLIES 11
Peter Nikitka
Honored Contributor

Re: Comparing two files in hp-ux

Hi,

the diff does the comparation - right?
It seems, you want to something more - what?

The 'man diff' gives you options for modifying output or even (-e') generates ed-commands to create file2 from file1.

See 'man sdiff' for a side-by-sside diff.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: Comparing two files in hp-ux

Hi:

You probably want to use 'comm(1)'. See the manpages for more information.

Regards!

...JRF...
Ivan Krastev
Honored Contributor

Re: Comparing two files in hp-ux

Use grep:

grep -vf file1 file2

regards,
ivan
harry d brown jr
Honored Contributor

Re: Comparing two files in hp-ux


What is the proper output you expect?

and what output are you receiving?
and what inputs are you using?

live free or die
harry
Live Free or Die
vvsha
Frequent Advisor

Re: Comparing two files in hp-ux

I have two files and both the files has some data inside, and some of the data are similer also.

I want to find out what is the data in the first file which is not present in the second file.

Is there any way to find out?

Please help me on this

F Verschuren
Esteemed Contributor

Re: Comparing two files in hp-ux

is this wat you like:
sort -n file1.txt > 1
sort -n file2.tx1 > 2
diff 1 2 |grep ^"<"
harry d brown jr
Honored Contributor

Re: Comparing two files in hp-ux

Like JRF pointed out the comm command:

comm -3 file1.txt file2.txt

Provided both files are sorted.

live free or die
harry
Live Free or Die
Arturo Galbiati
Esteemed Contributor

Re: Comparing two files in hp-ux

Hi,
sort file1 -o file1.sorted
sort file2 -o file2.sorted
grep vf file1.sorted fle2.sorted
HTH,
Art
Dennis Handly
Acclaimed Contributor

Re: Comparing two files in hp-ux

As others have mentioned, you can use comm(1) or grep -vf to find all lines in the second but not the first.

>harry: comm -3 file1.txt file2.txt

A slight correction, comm -13 will only print the lines from file 2.

>Arturo: sort file1 -o file1.sorted

I'm not sure why you want to sort the files unless you want to use comm(1)? You may want to use sort -u, if you don't want to see you have N lines (all the same) not in file1.

Note: Using sort (twice) and comm would be faster than grep -v, if you have 10s of thousands of lines.