Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ignore line endings when comparing files?

Tags:

diff

I am comparing two text files and I get the following result:

diff file1 file2 | grep 12345678  > 12345678 < 12345678 

As you can see, the same string exists in both files, and both files were sorted with sort.

The line endings must be getting in the way here (Windows vs. Unix).

Is there a way to get diff to ignore line endings on Unix?

like image 712
vikingsteve Avatar asked Dec 05 '16 12:12

vikingsteve


People also ask

What is difference between LF and CRLF?

CR = Carriage Return ( \r , 0x0D in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line. LF = Line Feed ( \n , 0x0A in hexadecimal, 10 in decimal) — moves the cursor down to the next line without returning to the beginning of the line.

How do I view Crlf in vi?

vi shows newlines (LF character, code x0A ) by showing the subsequent text on the next line. Use the -b switch for binary mode. For example , vi -b filename or vim -b filename -- . It will then show CR characters ( x0D ), which are not normally used in Unix style files, as the characters ^M .

What does M mean in git diff?

^M represents carriage return. This diff means something removed a Unicode BOM from the beginning of the line and added a CR at the end.


1 Answers

Use the --strip-trailing-cr option:

diff --strip-trailing-cr file1 file2 

The option causes diff to strip the trailing carriage return character before comparing the files.

like image 72
Ruslan Osmanov Avatar answered Oct 15 '22 05:10

Ruslan Osmanov