Search code examples
linuxawksedhead

head file with windows line endings


On my ContOS server, I have several huge files with windows line endings. Which, I need the first n lines from in order to run some tests.

I've tried a few standard "linux" ways of doing it:

head -10 file.dat

And

sed -n 1,10p file.dat

And

awk 'NR <=10' file.dat

All of which produce don't respect the windows line endings and simply output the entire file.

Is there a way to get the n lines of a file with windows line endings?

Also, it should be noted that the output should still have the windows line endings.


Solution

  • This wouldn't happen with Windows line endings, which are CRLF, since Unix uses LF. So the LF would still be seen and used.

    What you're describing would happen if the line endings were just CR without LF. You can translate this with:

    tr '\r' '\n' < file.dat | head -10 | tr '\n' '\r'
    

    The first tr converts to Unix format, and the second one translates back to the original format.