Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a quick way to detect the EOL for a file?

I normally just open up the file in Vim and check out the status line. I'm sure there's a better and quicker way.

I figured a grep might work, but I am not sure if it's the most elegant way to detect the EOLs. I'm specifically focused on detecting the Mac EOL (CR), Windows (CRLF) and Unix (LF).

A a *nix/Bash solution is preferred, but I am interested in other platforms as well.

(Earlier, I answered a Server Fault question that could possibly be an EOL issue, and it got me wondering if there was a quick way.)

share|improve this question
    
I guess you could get it with (\r|\n|\r\n), but like you say, that's not very elegant. –  Phoshi Oct 24 '10 at 22:07
    
What are the 3 possible EOLs? –  barlop Oct 24 '10 at 22:12
    
Mac (CR), Windows(CRLF) and Linux (LF). I'm sure there's more out there but those are the EOL's I care about. I'll modify the question. –  Beaming Mel-Bin Oct 24 '10 at 22:17
add comment

1 Answer

up vote 8 down vote accepted

file (at least with the magic database commonly found on Linux) is an easy way. It won't warn you about mixed-ending files, though.

$ file unix mac dos  # the files have line endings matching their names
unix: ASCII text
mac:  ASCII text, with CR line terminators
dos:  ASCII text, with CRLF line terminators
share|improve this answer
    
Find this one hard to beat. –  Beaming Mel-Bin Oct 24 '10 at 23:22
    
'file' is not a reliable way, at least not without extra parameters for 'file'. On an XML file, it will only output "XML document text", no matter the EOL character sequence (observed on MinGW 1.0.17, 2011-05-30). (I ran into this problem at work.) –  Peter Mortensen May 1 '12 at 9:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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