8

given a text, is there a program that can detect what the new line character is, the Windows, Mac, or Linux one?

Eg, the following text is shown by less

memor:^M^Mis storage directly accessible by CPU^M^MThe term "memory" is often (but not always) associated with addressable semiconductor memory, i.e. integrated circuits consisting of silicon-based transistors.^MThe semiconductor memory i s organized into ^Mmemory cells

I thought ^M is the new line character under Mac. But in emacs, I searched by Ctrl-q Ctrl-m and found nothing.

Thanks.

  • 1
    file command sould give a clue, dos2unix might also help. – Archemar Oct 29 '14 at 5:47
9

file will report on line endings for text files. If you run file on an ASCII Unix/Linux/OSX text file, for example, it will report simply:

$ file test.txt
test.txt: ASCII text

If, however the file has DOS/Windows line-endings, it will instead report:

test.dos: ASCII text, with CRLF line terminators

Up to OS 9, Macs used CR as the line ending. When presented with such a file, file will report:

test.mac: ASCII text, with CR line terminators

With OSX, Macs converted to standard Unix line endings.

  • If a text file is a mixture of text from Linux, Macs, and Windows. Will file still report the right new line character? – Tim Oct 29 '14 at 12:25
  • @Tim I tested it on some mixed files and it correctly reported mixed endings. For example, with all three types, it reported ASCII text, with CRLF, CR, LF line terminators. – John1024 Oct 29 '14 at 16:56
  • I tried this and it gave me ASCII text, with very long lines. The terminators are LF – wordsforthewise Mar 1 '17 at 17:42
  • @wordsforthewise LF terminators are the unix and linux standard. If a file has LF terminators, then file doesn't mention what terminators are used. See the first example in the answer above. Terminators are only mentioned if the file has non-unix terminators. See the second and third examples in the answer above. Separately, it is likely that your file has one or more long lines. – John1024 Mar 1 '17 at 18:14
  • 1
    Yes, I understand. I just thought it was funny it said 'very long lines' – wordsforthewise Mar 1 '17 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.