Converting newline formatting from Mac to Windows
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.
Yarin
Products PDF Buddy - Popular online PDF editor Gems Snappconfig - Smarter Rails app configuration
Updated on May 01, 2020Comments
-
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 about 13 yearsunix2dos/dos2unix do not exist on my mac and I haven't found any place to install them- Do you know of any?
-
Ergwun about 12 yearsThe
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. Theperl
command works though. -
pbr over 11 yearsThe 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 about 11 years@mgadda: +1 - yes, I switched to homebrew from MacPorts a while back now and haven't looked back.
-
John over 10 yearsOSX 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 over 10 yearsbe 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 over 10 yearsThe perl versions don't work for me on windows, because the -p switch doesn't use
binmode
, andreadline
will convert the"\r\n"
to"\n"
. -
RandomInsano about 10 yearsTried perl and sed, didn't work (I could have figured it out, wasn't worth a try). This worked great.
-
Daryl Spitzer almost 10 yearsThis 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 over 9 yearsHuge thanks for showing "Convert to Unix". I was after that way and your double answer helped me, and got my upvote.
-
UserYmY over 9 yearsthis deletes all of the line breaks I actually still need to have line breaks but with \n
-
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. Usehexdump
or similar to find out exactly what sort of end-of-line convention is now used in the file. -
Mike Robinson about 9 yearsAlso 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 usingsed
(and-i
) to do any sort of conversion of this kind is a very good one, because, unliketr
, you are not limited to "one character at a time." -
Kat almost 9 yearsAs 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 usingsudo
). I had to write the file elsewhere and then move it into the/etc
directory. -
Pratyush almost 9 yearsUse Homebrew to get the dos2unix and unix2dos packages instead.
-
LarsH over 8 yearsOS X Yosemite still has the same problem with
sed
, but you can work around it without installing Homebrew, gnu-sed or unix2dos: Usesed -e 's/$/^M/' inputfile > outputfile
, where^M
is a control character produced on the command line viaCtrl+V Ctrl+M
. -
Geoff about 8 yearsFor anyone that comes across this now, the Homebrew formula is now called
dos2unix
. You'll want tobrew install dos2unix
. -
Steven Hirlston about 8 yearsActually, either
brew install unix2dos
orbrew install dos2unix
work fine. They install the same package. Use whichever name speaks to you :) -
Fang almost 8 yearsOr with Macports:
port install dos2unix
. -
jcsahnwaldt Reinstate Monica over 5 yearsAnother 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.html for details.