Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Mac vs. UNIX line breaks UNIX
If you're having trouble getting something to work right, and that something is based on a text file (for example, a Perl CGI script or a page of HTML), make sure you don't have any hidden Mac line breaks (also called line endings) in the file!

If you installed or modified the file using a GUI-based tool, there's a chance you accidentally inserted Mac line breaks. I know (from personal experience!) that Mac line breaks can make both Perl scripts and static HTML pages non-functional if they show up in the wrong spot. Read the rest to see how to find and remove the line breaks...

How can you tell? Start a terminal and open the file with the vi text editor. It's important to use vi, as the other editors will hide control keys from sight.

If you see a bunch of ^M's in the file, you have Mac line breaks. There are some shell commands [editor's note - see the comments for a one-line perl command to fix your files!] that can auto-replace them, but I just used a Mac text editor (either jedit, BBedit, or Alpha) and re-saved the file, making sure to specify UNIX as the file type.

This problem can be very difficult to diagnose, especially if you normally edit in Emacs, and try just cat'ing the file at the command line. In both cases, you will not see the ^M's, and won't understand why your file isn't doing what it should!
  • Currently 2.43 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (7 votes cast)

Mac vs. UNIX line breaks | 18 comments | Create New Account
Click here to return to the 'Mac vs. UNIX line breaks' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
fixing Mac line breaks w/ PERL
Authored by: Anonymous on Dec 07, '00 03:28:27PM

There are many places where PERL is incredibly handy. This is one of them. You can use
PERL on the command line to change the line ending ( a '\r' (Mac) to a '\n' (UNIX) ):

perl -pi -e 's/\r/\n/g' <filename>

I have put an alias in my .bashrc file that looks like this:

alias fle="perl -pi -e 's/\r/\n/g' "

Now I just type fle <filename> and everything works file. I use "fle" for "fix line endings," but
yo can use anything you want to.

[ Reply to This | # ]
fixing Mac line breaks w/ PERL
Authored by: robg on Dec 07, '00 03:40:38PM

Perl is one thing that I would definitely like to learn more about. Thanks for showing me the
easier method!


[ Reply to This | # ]
whre is .bashrc
Authored by: vic on Apr 10, '01 08:16:40PM

how did u add the shortcut to .bashrc ?
and where and how can i do this ?
where is .bashrc

[ Reply to This | # ]
whre is .bashrc
Authored by: foamy on Apr 10, '01 09:34:18PM

If you didn't install the BASH shell, then you won't have a .bashrc.

If you are using the default shell for the terminal (tcsh I think), then you should have a .tcshrc file in /Users/username/ directory. To check, open the terminal and type ls -al and hit return. If it is there you can edit it using by typing pico .tcshrc. If it is not there, pico .tcshrc will create it.

Enter you're aliases in the format
alias name "command in quotes" for example
alias dante "telnet"
so when you type dante at the terminal, it will open a telnet session to

You have to quit the terminal and restart it to see the effects.

if you are using the zsh shell (a lot like BASH I'm told), then the syntax is
alias name='command'

have fun

[ Reply to This | # ]
fixing Mac line breaks w/ PERL (without blank lines)
Authored by: elindal on Aug 23, '01 01:37:48AM

Using this can result in there being blank lines between each and every line that was there before.

To prevent this use

alias fle="perl -pi -e 's/\r\n?/\n/g' "

The difference here is the n? which tells to to also replace existing n that is there as long as it is attached to a r.

I hope this helps people

Richard Canning

[ Reply to This | # ]
fixing Mac line breaks w/ PERL (MULTIPLE FILES)
Authored by: CrazyDuke on Apr 20, '02 02:00:52PM

I found that I needed to do like 100 at a time and it wasn't going to cut it to pass one file at a time. This is what I did:

1. if you don't have a bin directory in your home directory make one go to the terminal and tyep "mkdir bin"
2. the in the bin directory create a file called 'fle' with your favorite editor and then put in this one line:

for T in `ls -a $*`; do perl -pi -e 's/rn?/n/g' $T; done

I chose "T" for no particular reason.

3. Save he file and in the terminal type: chmod 755 fle while in the bin directory.
4. If you want to use it right away, you need to first close whatever terminal window you're using and open a new one. For some reason when creating new shell scripts this has to be done before you can execute them.

That's it. Now when you want to do a whole directory you can do:

fle * (or simply fle by itself)

or specify file-names: fle *.php or fle myfile.txt

it will work with one or mulitple files.

[ Reply to This | # ]
fixing Mac line breaks w/ PERL (MULTIPLE FILES)
Authored by: Seth Milliken on Sep 04, '03 02:25:39PM

You don't have to close your terminal. Just type "rehash". Note also that the path to your bin directory has to be in your PATH for this to work.

[ Reply to This | # ]
fixing Mac line breaks w/ PERL (MULTIPLE FILES)
Authored by: mmarlett on Jan 02, '05 05:01:04PM

A somewhat more helpful (for me) version of this script reads like this:

for filename in *.TXT
         echo $filename
         perl -pi -e 's/rn?/n/g' "$filename"

The -li is unnecessary and the files I happen to want to deal with are of the TXT sort. However, * would cover everything as well.

The important change, though, is the quotes around $filename (or $T in the above example) so that file names with spaces in them are handled correctly by perl.

[ Reply to This | # ]

fixing Mac line breaks w/ PERL (MULTIPLE FILES)
Authored by: beatmacman on Oct 02, '06 09:30:15AM
Actually, even easier command, using find:

find . -type f -name "DEMO*" -exec perl -pi -e 's/\r/\n/g' \{\} \;

this changes all files starting with "DEMO". Obviously, you can change that to "*" or whatever suits your needs.

[ Reply to This | # ]
Deconstructing that Perl
Authored by: Gee4orce on Dec 08, '00 10:44:04AM

Thought you might like to know how that Perl works:

perl -pi -e 's/\r/\n/g' <filename>

OK let's break that down:

perl - pretty obvious, runs the Perl interpreter

-p - says to perl, 'for every line in the file, do this...'
-i - says to perl, 'send output back to the same file you read from'

-e - says 'run the next bit as if it's a script'

s/\r/\n/g : This is the bit that does the work

s// - the substitute command, The '/' are just separators
\r - a 'return'
\n - a 'newline
g - means 'global', ie for every ocurrance.

So, put that together, and it means 'substitute every r with n'

(PS - I couldn't get the backslashes to display in this message !)


[ Reply to This | # ]
Authored by: robg on Dec 08, '00 11:25:07AM

Yea, it's a bit of a problem due to a bug in the way that Geeklog handles them at present.
The only way to make sure they show up (along with frontslashes, brackets, quotes, etc) if
you post in HTML is to encode them:

&#039; = '
&#047; = /
&#060; = <
&#062; = >
&#092; =

I edited your post in the database to insert the characters so it reads correctly. This bug will
hopefully be addressed in the next release...

[ Reply to This | # ]
you can use vi too
Authored by: iMike on Jan 04, '01 06:10:10AM

You can also use vi to replace the Mac line breaks using the following command:


NOTE: You will need to type shift+: to get to the colon prompt
in vi you can then enter the command starting with the % symbol.
The ^M actually maps to control+shift+v followed by control+shift+m
(+'s are not included), so in actuality the command looks like this:

:%s/control V control M/control V control M/g

Many thanks to my former Perl teacher for this one--
I used it a whole lot of times in my Perl class. 8^)


[ Reply to This | # ]
A way to do it with SED
Authored by: Anonymous on May 30, '01 10:48:43AM
sed 'y/^M/n/' filename  newfilename
To Enter the '^M' character: type Cntrl-V then Cntrl-M. HTH -Excalibur

[ Reply to This | # ]
Command line / shell variant
Authored by: osolemirnix on Jun 24, '02 11:33:38AM

Use the following shell command:

tr -d 'r' < file.txt > file.txt

This will remove the carriage returns (^M) and leave only the unix linefeeds.


tr 'r' 'n' < file.txt > file.txt

This will replace the carriage returns (^M) with unix linefeeds.

[ Reply to This | # ]
Command line / shell variant
Authored by: dsewell on Feb 09, '06 10:01:54AM
tr -d 'r' < file.txt > file.txt
tr 'r' 'n' < file.txt > file.txt
The escaping backslashes got stripped in these examples. They should be:
tr -d '\r' < file.txt > file.txt
tr '\r' '\n' < file.txt > file.txt

David Sewell
White Hall, Virginia

[ Reply to This | # ]

Re: Mac vs. UNIX line breaks
Authored by: sjk on Jun 25, '02 05:32:03AM
Annoyed with seeing ^M end-of-lines when viewing files with less on Mac OS X, I wrote this simple alias:

alias macless 'tr "\r" "\n" < \!^ |& less'

Would be convenient if that capability were an option to "relevant" command-line utilities, like diff.

[ Reply to This | # ]
Re: Mac vs. UNIX line breaks
Authored by: Detrius on Oct 03, '06 08:42:38PM

Install TextWrangler and use twdiff instead. :)

[ Reply to This | # ]
Authored by: Ptitboul on Oct 22, '03 06:05:53PM
There is also the "recode" command line utility, that can be installed with fink. recode can do end-of-line translations, but also charset translations. For example you may want to do recode mac/CR..utf-8 to translate a file that has Mac end-of-lines and MacRoman character encoding (typical of MacOS 9) to a file that has unix end-of-line and UTF-8 character encoding (typical of MacOS X).

[ Reply to This | # ]