2

How to check whether lines in a text file end with CRLF or LF?

  • 3
    Type file <filename>. If it makes no mention, it's LF; if it's CRLF, it'll say "with CRLF line terminators". – Tom Hunt Dec 01 '15 at 17:49
  • Are you asking how to detect which line endings are used in a file? Or are you asking how to process a file which contains both LF and CRLF line endings? Please edit your question to clarify. – RobertL Dec 01 '15 at 20:27

2 Answers2

7

As Tom Hunt said, you can use file and it will tell you if the lines end with CRLF.

Or you can use GNU cat -A filename to visually inspect the file. Lines ending in CRLF will look like:

blahblahblah^M$

A more portable alternative is to use sed -n l filename. Lines ending with CRLF will look like:

blahblahblah\r$
cas
  • 78,579
0

I didn't know about the cat or sed ways, but I use this:

tr "\r" "@" < $FILE | grep --color "@$"

which replaces the CR with a @ (vim's ":set list" style) and shows the resulting @'s at the end of each line in color.

AlvaroGMJ
  • 241