Converting Windows / DOS / Linux line endings

Quick ways to convert Windows/DOS style line endings (CRLF / \r\n) with Unix style (LF / \n).

If the dos2unix / unix2dos commands are available, they're a quick, simple way to do it.

If not, the following commands ought to do it:

sed -e 's/$/\r/' inputfile > outputfile  # Unix to DOS
sed -e 's/\r//'  inputfile > outputfile  # DOS to Unix

# or to get sed to make the changes to the file directly (edit in-place),
# use the -i option... this means you can also glob to apply it to several
# files:
sed -ie 's/$/\r/' *.pl   # Unix to DOS
sed -ie 's/\r//'  *.pl   # DOS to Unix

# or the Perl way:
perl -p -e 's/(\r\n|\n|\r)/\r\n/g' inputfile > outputfile  # Convert to DOS
perl -p -e 's/(\r\n|\n|\r)/\n/g'   inputfile > outputfile  # Convert to UNIX
perl -p -e 's/(\r\n|\n|\r)/\r/g'   inputfile > outputfile  # Convert to old Mac
 
linux/linendings.txt · Last modified: 2010/02/26 10:45 (external edit)
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki