Operating System - Linux
1820708 Members
2849 Online
109627 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.
Elmar P. Kolkman
Honored Contributor

Re: Comparing two files in hp-ux

There is a slight problem with the grep solution: if you have a line in file1 containing a subset of a line in file2, it will still skip the line in file2.

Example:
file1 contains a line consisting of a single '1'
Then all lines containing a '1' in file2 are skipped.

comm handles this better.
Also: mind that you want to find data in file1 that's not in file2. Meaning you want to do it the other way around: skip lines in file 1 that's not in file2.

A solution for the grep problem is to create a temporary file, containing all lines from file2 with a ^ prepended and a $ appended to each line.

Or use the -w flag for grep.
Every problem has at least one solution. Only some solutions are harder to find.
Dennis Handly
Acclaimed Contributor

Re: Comparing two files in hp-ux

>Elmar: There is a slight problem with the grep solution
>Or use the -w flag for grep.

Oops, right. The -x flag compares the entire line. Unfortunately it still looks at patterns so you need fgrep:
fgrep -xvf file1 file2