Convert line formatting between Unix and Windows

Windows uses carriage return and line feed for newline:

\r\n

Unix only uses Line feed for newline:

\n

In conclusion, simply replace every occurence of \n by \r\n. Both unix2dos and dos2unix are not by default available on Mac OSX.
Fortunately, you can simply use Perl or sed to do the job:

sed -e 's/$/\r/' infile > outfile                # UNIX to DOS  (adding CRs)
sed -e 's/\r$//' infile > outfile                # DOS  to UNIX (removing CRs)
perl -pe 's/\r\n|\n|\r/\r\n/g' infile > outfile  # Convert to DOS
perl -pe 's/\r\n|\n|\r/\n/g'   infile > outfile  # Convert to UNIX
perl -pe 's/\r\n|\n|\r/\r/g'   infile > outfile  # Convert to old Mac
Share Button

Leave a comment

Your email address will not be published. Required fields are marked *