New line characters in text files between Mac and Windows

Sahil Malik
Winsmarts.com
Published in
1 min readDec 27, 2016

--

You may have noticed that a text file that contained the following on a Mac,

This is
a
text
file

ends up looking like

This isatextfile

on windows ..

Why?

Because Windows uses \r\n for new line, and Unix uses \n. I’d bitch but frankly Unix right here. But whatever! So how do we fix it?

Classically you have “unix2dos” and “dos2unix” commands to fix this problem, and Mac does not have those commands.

You can install them however using brew.

brew install unix2dos
brew install dos2unix

Or you can use “sed” to do the job for you, like this.

sed -e 's/$/\r/' inputfile > outputfile # unix2dos
sed -e 's/\r$//' inputfile > outputfile # dos2unix

Or you can do it using perl,

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

--

--