Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting newline formatting from Mac to Windows

People also ask

How do I convert a Mac file to Windows format?

On your MacOpen Migration Assistant, which is in the Utilities folder of your Applications folder. Follow the onscreen prompts until you're asked how you want to transfer your information. Select the option to transfer from a Windows PC, then click Continue. Select the icon representing your PC, then click Continue.

How do you change line endings on a Mac?

Usage is simple, just use one of the flags: -m, -d, or -u, for traditional Mac, DOS/Windows, or Unix line endings. A -t option will just tell you the type of a file. For example, flip -d *. txt will convert all files ending in .


Windows uses carriage return + 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/' inputfile > outputfile                # UNIX to DOS  (adding CRs)
sed -e 's/\r$//' inputfile > outputfile                # DOS  to UNIX (removing CRs)
perl -pe 's/\r\n|\n|\r/\r\n/g' inputfile > outputfile  # Convert to DOS
perl -pe 's/\r\n|\n|\r/\n/g'   inputfile > outputfile  # Convert to UNIX
perl -pe 's/\r\n|\n|\r/\r/g'   inputfile > outputfile  # Convert to old Mac

Code snippet from:
http://en.wikipedia.org/wiki/Newline#Conversion_utilities


This is an improved version of Anne's answer -- if you use perl, you can do the edit on the file 'in-place' rather than generating a new file:

perl -pi -e 's/\r\n|\n|\r/\r\n/g' file-to-convert  # Convert to DOS
perl -pi -e 's/\r\n|\n|\r/\n/g'   file-to-convert  # Convert to UNIX

You can install unix2dos with Homebrew

brew install unix2dos

Then you can do this:

unix2dos file-to-convert

You can also convert dos files to unix:

dos2unix file-to-convert

Just do tr delete:

tr -d "\r" <infile.txt >outfile.txt