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
?
If you don't have access to dos2unix, but can read this page, then you can copy/paste dos2unix.py from here.
Cross-posted from superuser.
I tried sed 's/^M$//' file.txt on OSX as well as several other methods (http://www.thingy-ma-jig.co.uk/blog/25-11-2010/fixing-dos-line-endings or http://hintsforums.macworld.com/archive/index.php/t-125.html). None worked, the file remained unchanged (btw Ctrl-v Enter was needed to reproduce ^M). In the end I used TextWrangler. Its not strictly command line but it works and it doesn't complain.
Doing this with POSIX is tricky:
POSIX Sed does not support
\r
or\15
. Even if it did, the in place option-i
is not POSIXPOSIX Awk does support
\r
and\15
, however the-i inplace
option is not POSIXd2u and dos2unix are not POSIX utilities, but ex is
POSIX ex does not support
\r
,\15
,\n
or\12
To remove carriage returns:
To add carriage returns:
An even simpler awk solution w/o a program:
Technically '1' is your program, b/c awk requires one when given option.
UPDATE: After revisiting this page for the first time in a long time I realized that no one has yet posted an internal solution, so here is one: