Converting newline formatting from Mac to Windows

190,989

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

Just do tr delete:

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

Solution 5

You probably want unix2dos:

$ man unix2dos

NAME
       dos2unix - DOS/MAC to UNIX and vice versa text file format converter

SYNOPSIS
           dos2unix [options] [-c CONVMODE] [-o FILE ...] [-n INFILE OUTFILE ...]
           unix2dos [options] [-c CONVMODE] [-o FILE ...] [-n INFILE OUTFILE ...]

DESCRIPTION
       The Dos2unix package includes utilities "dos2unix" and "unix2dos" to convert plain text files in DOS or MAC format to UNIX format and vice versa.  Binary files and non-
       regular files, such as soft links, are automatically skipped, unless conversion is forced.

       Dos2unix has a few conversion modes similar to dos2unix under SunOS/Solaris.

       In DOS/Windows text files line endings exist out of a combination of two characters: a Carriage Return (CR) followed by a Line Feed (LF).  In Unix text files line
       endings exists out of a single Newline character which is equal to a DOS Line Feed (LF) character.  In Mac text files, prior to Mac OS X, line endings exist out of a
       single Carriage Return character. Mac OS X is Unix based and has the same line endings as Unix.

You can either run unix2dos on your DOS/Windows machine using cygwin or on your Mac using MacPorts.

Share:
190,989
Yarin
Author by

Yarin

Products PDF Buddy - Popular online PDF editor Gems Snappconfig - Smarter Rails app configuration

Updated on May 01, 2020

Comments

  • Yarin
    Yarin about 4 years

    I need a conversion utility/script that will convert a .sql dump file generated on Mac to one readable on Windows. This is a continuation of a problem I had here. The issue seems to be with newline formatting in text files, but I can't find a tool to make the conversion...

  • Yarin
    Yarin about 13 years
    unix2dos/dos2unix do not exist on my mac and I haven't found any place to install them- Do you know of any?
  • Ergwun
    Ergwun about 12 years
    The sed command for UNIX to DOS does not work for me on OS X Lion - it just inserts the text "r" at the end of each line. The perl command works though.
  • pbr
    pbr over 11 years
    The awesome thing about these scripts is that they show, with the regular expressions, EXACTLY what the end-of-line conversion needs to be to convert to either format, starting from anything.
  • Paul R
    Paul R about 11 years
    @mgadda: +1 - yes, I switched to homebrew from MacPorts a while back now and haven't looked back.
  • John
    John over 10 years
    OSX uses older version of sed. I use Homebrew for OSX, and installed gnu-sed. You use with the "gsed" command instead of "sed". That works.
  • Dennis
    Dennis over 10 years
    be careful with this on certain Cygwin/git bash installations on Windows systems. This may give you Can't do inplace edit on file: Permission denied., and delete the file. Look into other utilities instead.
  • gatinueta
    gatinueta over 10 years
    The perl versions don't work for me on windows, because the -p switch doesn't use binmode, and readline will convert the "\r\n" to "\n".
  • RandomInsano
    RandomInsano about 10 years
    Tried perl and sed, didn't work (I could have figured it out, wasn't worth a try). This worked great.
  • Daryl Spitzer
    Daryl Spitzer almost 10 years
    This was the first solution I found to BBEdit's line numbers not matching the count of lines as I read them using Python (and not matching wc -l).
  • null
    null over 9 years
    Huge thanks for showing "Convert to Unix". I was after that way and your double answer helped me, and got my upvote.
  • UserYmY
    UserYmY over 9 years
    this deletes all of the line breaks I actually still need to have line breaks but with \n
  • Mike Robinson
    Mike Robinson about 9 years
    "hints.macworld.com/article.php?story=20031018164326986" also has a good write-up on how to use the tr command to perform various conversions. Use hexdump or similar to find out exactly what sort of end-of-line convention is now used in the file.
  • Mike Robinson
    Mike Robinson about 9 years
    Also note that sed does understand backslash-escapes such as \r and ``\n` and therefore can also use these in the substitution. You don't actually have to input a literal control-M to refer to that character (or any other). The principle of using sed (and -i) to do any sort of conversion of this kind is a very good one, because, unlike tr, you are not limited to "one character at a time."
  • Kat
    Kat almost 9 years
    As an aside, when I tried using this to fix a hosts file, this failed for me because perl and sed both couldn't write the file (despite using sudo). I had to write the file elsewhere and then move it into the /etc directory.
  • Pratyush
    Pratyush almost 9 years
    Use Homebrew to get the dos2unix and unix2dos packages instead.
  • LarsH
    LarsH over 8 years
    OS X Yosemite still has the same problem with sed, but you can work around it without installing Homebrew, gnu-sed or unix2dos: Use sed -e 's/$/^M/' inputfile > outputfile, where ^M is a control character produced on the command line via Ctrl+V Ctrl+M.
  • Geoff
    Geoff about 8 years
    For anyone that comes across this now, the Homebrew formula is now called dos2unix. You'll want to brew install dos2unix.
  • Steven Hirlston
    Steven Hirlston about 8 years
    Actually, either brew install unix2dos or brew install dos2unix work fine. They install the same package. Use whichever name speaks to you :)
  • Fang
    Fang almost 8 years
    Or with Macports: port install dos2unix.
  • jcsahnwaldt Reinstate Monica
    jcsahnwaldt Reinstate Monica over 5 years
    Another workaround for Mac OS (tested on 10.13.6 High Sierra): Place a $ before the single quote containing the sed command: sed $'s/\r$//' Explanation: bash decodes backslash-escapes in $'...' strings. See gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.ht‌​ml for details.