How can I programmatically (i.e., not using vi
) convert DOS/Windows newlines to Unix?
The dos2unix
and unix2dos
commands are not available on certain systems. How can I emulate these with commands like sed
/awk
/tr
?
How can I programmatically (i.e., not using vi
) convert DOS/Windows newlines to Unix?
The dos2unix
and unix2dos
commands are not available on certain systems. How can I emulate these with commands like sed
/awk
/tr
?
Super duper easy with PCRE;
As a script, or replace
$@
with your files.The solutions posted so far only deal with part of the problem, converting DOS/Windows' CRLF into Unix's LF; the part they're missing is that DOS use CRLF as a line separator, while Unix uses LF as a line terminator. The difference is that a DOS file (usually) won't have anything after the last line in the file, while Unix will. To do the conversion properly, you need to add that final LF (unless the file is zero-length, i.e. has no lines in it at all). My favorite incantation for this (with a little added logic to handle Mac-style CR-separated files, and not molest files that're already in unix format) is a bit of perl:
Note that this sends the Unixified version of the file to stdout. If you want to replace the file with a Unixified version, add perl's
-i
flag.As an extension to Jonathan Leffler's Unix to DOS solution, to safely convert to DOS when you're unsure of the file's current line endings:
This checks that the line does not already end in CRLF before converting to CRLF.
This worked for me
TIMTOWTDI!
Based on @GordonDavisson
One must consider the possibility of
[noeol]
...Since the question mentions sed, this is the most straight forward way to use sed to achieve this. What the expression says is replace all carriage-return and line-feed with just line-feed only. That is what you need when you go from Windows to Unix. I verified it works.