Discussion:
tell diff to ignore line numbers
Antoine Jacoutot
2003-12-22 16:24:09 UTC
Permalink
Hi :)

I know this is not the right newsgroup for this, but, first I run OpenBSD and
second, I have no idea where to ask for help about this.
Basically, I would like the diff command to output only the differences
between 2 ascii files, regardless of the line positions.
For instance:

# file1
azerty
qsdfgh
wxcvbn

# file 2
qsdfgh
azerty
wxcvbn

I would like those two files to be considered as having no differences (using
diff or any other tool), so the output should be blank (only the lines
position are different).
Is it even possible using standard tools ?

Thanks in advance, and sorry if this is really to much OT.
Regards,

Antoine
Paul de Weerd
2003-12-22 16:36:31 UTC
Permalink
On Mon, Dec 22, 2003 at 05:24:09PM +0100, Antoine Jacoutot wrote:
| Hi :)
|
| I know this is not the right newsgroup for this, but, first I run OpenBSD and
| second, I have no idea where to ask for help about this.
| Basically, I would like the diff command to output only the differences
| between 2 ascii files, regardless of the line positions.
| For instance:
|
| # file1
| azerty
| qsdfgh
| wxcvbn
|
| # file 2
| qsdfgh
| azerty
| wxcvbn
|
| I would like those two files to be considered as having no differences (using
| diff or any other tool), so the output should be blank (only the lines
| position are different).
| Is it even possible using standard tools ?

Of course it is ! Try sorting your files prior to diffing them.
sort file1 > file1.sort
sort file2 > file2.sort
diff file1.sort file2.sort

Hope that helps.

Cheers,

Paul 'WEiRD' de Weerd
--
++++++++[<++++++++++>-]<+++++++.>+++[<------>-]<.>+++[<+
+++++++++++>-]<.>++[<------------>-]<+.--------------.[-]
http://www.weirdnet.nl/
Todd C. Miller
2003-12-22 21:55:41 UTC
Permalink
Post by Paul de Weerd
Of course it is ! Try sorting your files prior to diffing them.
sort file1 > file1.sort
sort file2 > file2.sort
diff file1.sort file2.sort
If you are going to sort the files you might as well use comm(1)
instead of diff(1) -- it will be much faster.

- todd
Antoine Jacoutot
2003-12-22 23:10:15 UTC
Permalink
Post by Todd C. Miller
If you are going to sort the files you might as well use comm(1)
instead of diff(1) -- it will be much faster.
Hey this is great, thank you.

Antoine
Peter N. M. Hansteen
2003-12-22 16:30:40 UTC
Permalink
Post by Antoine Jacoutot
I would like those two files to be considered as having no differences (using
diff or any other tool), so the output should be blank (only the lines
position are different).
For the tiny samples you provided, comparing sorted versions of the
data would give you what you want. You could for example use sort(1)
to create sorted (temporary?) versions which are then fed to diff.
Post by Antoine Jacoutot
Is it even possible using standard tools ?
Hard to say what happens when you are confronted with real-world data,
but simple tools like sort and the grep family are certainly handy.

- P
--
Peter N. M. Hansteen, member of the first RFC 1149 implementation team
http://www.blug.linux.no/rfc1149/ http://www.datadok.no/
"First, we kill all the spammers" The Usenet Bard, "Twice-forwarded tales"
Hanford, Seth
2003-12-22 16:43:07 UTC
Permalink
Post by Antoine Jacoutot
Hi :)
hi.
Post by Antoine Jacoutot
Basically, I would like the diff command to output only the differences
between 2 ascii files, regardless of the line positions.
try:
man sort
man diff
Post by Antoine Jacoutot
I would like those two files to be considered as having no differences (using
diff or any other tool), so the output should be blank (only the lines
position are different).
Is it even possible using standard tools ?
yes
standard tools + pipes, maybe a temp file
Antoine Jacoutot
2003-12-22 17:09:38 UTC
Permalink
t1=`mktemp`
t2=`mktemp`
sort -o $t1 file1
sort -o $t2 file2
diff $t1 $t2
Allright, indeed :)
I figured I should use diff and sort... but I was trying to use pipes instead
of tmp files.

t1=`mktemp -u`; t2=`mktemp -u`; sort -o $t1 file1 ; sort -o $t2 file2; diff
$t1 $t2

works great, and with the -u flags, I guess it is more secure.
If anyone could find a way to achieve this without using temporary files, this
would be great.

Thanks a lot to all the people who answered.

Regards,

Antoine
Greg Wooledge
2003-12-23 00:16:02 UTC
Permalink
Post by Antoine Jacoutot
t1=`mktemp`
t2=`mktemp`
sort -o $t1 file1
sort -o $t2 file2
diff $t1 $t2
If anyone could find a way to achieve this without using temporary files, this
would be great.
In bash, you can do this:

diff <(sort file1) <(sort file2)

However, this syntax does not work for me in ksh. I don't know whether
ksh has any equivalent feature.
--
Greg Wooledge | "Truth belongs to everybody."
***@wooledge.org | - The Red Hot Chili Peppers
http://wooledge.org/~greg/ |
jmc
2003-12-23 12:19:59 UTC
Permalink
Post by Greg Wooledge
diff <(sort file1) <(sort file2)
However, this syntax does not work for me in ksh. I don't know whether
ksh has any equivalent feature.
It is called "process substitution". As far as I can see, pdksh cannot
handle such constructions (I cannot even find anything about this in
POSIX).

jmc./
--
Gae yer neighbours aw chances.
Antoine Jacoutot
2003-12-23 13:38:09 UTC
Permalink
Post by jmc
It is called "process substitution". As far as I can see, pdksh cannot
handle such constructions (I cannot even find anything about this in
POSIX).
And what about csh, do you know if it can handle those ?
I'm actually googleling for this and let everyone knows if I find something.

Antoine
jmc
2003-12-23 13:52:20 UTC
Permalink
Post by Antoine Jacoutot
Post by jmc
It is called "process substitution". As far as I can see, pdksh cannot
handle such constructions (I cannot even find anything about this in
POSIX).
And what about csh, do you know if it can handle those ?
I'm actually googleling for this and let everyone knows if I find something.
I don't think so. korn shell, zsh and bash have it. I'm not sure what
else.

jmc
--
Readin's fur improvin'.
Loading...