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

Click here to return to the 'fixing Mac line breaks w/ PERL' 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 | # ]