Operating System - HP-UX
1831618 Members
2092 Online
110027 Solutions
New Discussion

Re: commands for comparing two files ..

 
SOLVED
Go to solution
Manuales
Super Advisor

commands for comparing two files ..

Hi
I have to compare two files .. i used diff

diff -Cn file1 file 2
i see this sign "!" what does that mean?

what other command could help me to compare two files? in theory they should have the same information so i have to see why they are different .. the information are paths ..

please your help
thanks.
11 REPLIES 11
Steven Schweda
Honored Contributor
Solution

Re: commands for comparing two files ..

> I have to compare two files [...]

Where?

uname -a

> i see this sign "!" what does that mean?

That might depend on the (invisible) context.

man diff

> [...] the information are paths ..

That tells me less than you might think.
sangilak
Trusted Contributor

Re: commands for comparing two files ..

Hi,


As mentioned above, it is difficult to provide you with an exact answer without the actual diff output.

Nevertheless, a "!" represents a change between lines that correspond in the two files.

See: http://en.wikipedia.org/wiki/Diff


sangilak
James R. Ferguson
Acclaimed Contributor

Re: commands for comparing two files ..

Hi Manuales:

The '!' notation means that the line is *not* present. Just as you use '!' to negate an expression...

Using the '-C' or '-c' offers differences in context.

Look at the manpages and the files you are comparing.

Regards!

...JRF...
Manuales
Super Advisor

Re: commands for comparing two files ..

i am trying with

grep -vf file1 file2
i think it is easier than diff
James R. Ferguson
Acclaimed Contributor

Re: commands for comparing two files ..

Hi (again):

> grep -vf file1 file2
> i think it is easier than diff

Depending on your objective (and whether or not the files are sorted or not and/or have meaningful sequences of lines, suggests things like 'grep', 'diff', 'comm', or a bit of my favorite (Perl) come to mind for file "comparisons".

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: commands for comparing two files ..

Hi Manuales:

I would add that 'diff' has a larger value than merely reporting the differences in two files. The output of 'diff' can be used to create file2 from file1 using either 'ed(1)' or 'patch(1)'.

Have a look at the manpages for 'diff(1)' and for 'patch(1)' for more insight.

As a bit of UNIX history trivia, the original author of the 'patch' utility was one Larry Wall --- who later invented the the Perl language and who continues to shepard its evolution (with many others) today.

Regards!

...JRF...
Manuales
Super Advisor

Re: commands for comparing two files ..

Let me give you an example:

I have file "a"
cuatro
diez
dos
n
och
seis
siete
tres
uno

I have file "b"cinco
cuat
dos
nueve
t
tres


1.- i want to have the output which shows me the lines that I do not have in the file b being compared with file a, this will be the output i want to get through commands coded:
cuatro
diez
n
och
seis
siete
uno

2.- i want to have the output which shows me what words i do not have neither file a nor file b, it will be:
cuatro
diez
n
och
seis
siete
uno
cinco
cuat
nueve
t

please your support.

Manuales
Super Advisor

Re: commands for comparing two files ..

i think i have it with command "comm"

=====================
EJEMPLO
=========================
$ cat a
cuatro
diez
dos
n
och
seis
siete
tres
uno
$
$ cat b
cinco
cuat
dos
nueve
t
tres
I need to know what i have i "a" and not in "b"
$ comm -23 a b
cuatro
diez
n
och
seis
siete
uno

is this correct? i think yes.

James R. Ferguson
Acclaimed Contributor

Re: commands for comparing two files ..

Hi Manuales:

> I need to know what i have i "a" and not in "b"

You are correct. Since your files are sorted:

# comm -23 a b

That is, show all lines that appear in 'a' but not in 'b'.

Regards!

...JRF...
INH
Regular Advisor

Re: commands for comparing two files ..

Hi

# diff file1 file2
another way
# grep -vf file1 file2

Regards,
INH
Knowledge is power
Dennis Handly
Acclaimed Contributor

Re: commands for comparing two files ..

>INH: diff file1 file2
>grep -vf file1 file2

The problem with the first is the extra metadata being output.
The second has N*N compares vs just N. Of course you do have to sort both files.