tinyapps.org / blog

Convert between Mac, Unix, and DOS/Windows EOL characters (CR, LF, CRLF) #

Craig Stuart Sapp's flip converts ASCII files between between Mac (OS 9 and ealier), *nix (including OS X), and DOS/Windows newline formats. As the provided OS X binary was compiled in 10.3 for PowerPC, I've compiled a much smaller Intel binary in 10.5.  Screenshot

flip's source code also explains a method for converting line endings via Perl:
Mac to Unix:
   perl -i -pe 's/\015/\012/g' mac-file
Mac to DOS:
   perl -i -pe 's/\015/\015\012/g' mac-file
Unix to Mac:
   perl -i -pe 's/\012/\015/g' unix-file
Unix to DOS:
   perl -i -pe 's/\012/\015\012/g' unix-file
DOS to Unix:
   perl -i -pe 's/\015\012/\012/g' dos-file
DOS to Mac:
   perl -i -pe 's/\015\012/\012/g' dos-file
Here are a few more possibilities from Jacek Artymiak's Filtering files with tr:
Mac -> UNIX: tr '\r' '\n' < macfile > unixfile
UNIX -> Mac: tr '\n' '\r' < unixfile > macfile
DOS -> UNIX: tr -d '\r' < dosfile > unixfile
UNIX -> DOS: '{ print $0"\r" }' < unixfile > dosfile

/nix | Nov 08, 2009

Subscribe or visit the archives