luffe
luffe

Reputation: 1665

Compare/Diff two files with different line terminators

I have two text files I wish to make sure are the same, the problem is that file1 (SELECT_20150210.txt) is generated on a windows platform, and file2 (sel.txt) is generated on a mac, so the two files have different line terminating characters even though they look the same:

The first line:

Eriks-MacBook-Air:hftdump erik$ head -n 1 sel.txt
SystemState 0x04    25  03:03:48.800    O
Eriks-MacBook-Air:hftdump erik$ head -n 1 SELECT_20150210.txt
SystemState 0x04    25  03:03:48.800    O

cmp says they are different:

Eriks-MacBook-Air:hftdump erik$ cmp sel.txt SELECT_20150210.txt
sel.txt SELECT_20150210.txt differ: char 35, line 1

But it's only the terminating characters that differ:

Eriks-MacBook-Air:hftdump erik$ head -n 1 SELECT_20150210.txt | hexdump -C
00000000  53 79 73 74 65 6d 53 74  61 74 65 09 30 78 30 34  |SystemState.0x04|
00000010  09 32 35 09 30 33 3a 30  33 3a 34 38 2e 38 30 30  |.25.03:03:48.800|
00000020  09 4f 0d 0a                                       |.O..|
00000024
Eriks-MacBook-Air:hftdump erik$ head -n 1 sel.txt | hexdump -C
00000000  53 79 73 74 65 6d 53 74  61 74 65 09 30 78 30 34  |SystemState.0x04|
00000010  09 32 35 09 30 33 3a 30  33 3a 34 38 2e 38 30 30  |.25.03:03:48.800|
00000020  09 4f 0a                                          |.O.|
00000023

So is there a way to cmp or diff these two file and telling cmp to ignore the different line terminating character? Thank you

Upvotes: 1

Views: 203

Answers (2)

Eugeniu Rosca
Eugeniu Rosca

Reputation: 5315

ASSUMPTION: you don't want to alter the line-endings of the original files

To avoid creating temporary files, you could use process substitution:

diff my_unix_file <(dos2unix < my_dos_file)
diff my_unix_file <(sed 's/\r//' my_dos_file)
diff my_unix_file <(tr -d '\r' < my_dos_file)

UPDATE (Comments converted into answer): Some improvements done thanks to anishsane

Upvotes: 2

anubhava
anubhava

Reputation: 786111

On OSX you can use this diff:

diff osx-file.txt <(tr -d '\r' < win-file.txt)

tr -d '\r' < win-file.txt will strip r from win-file.txt.

Upvotes: 2

Related Questions