Mac newlines kill PHP... recursive change?


Mad Mod Mojo
I've moved all my web projects over to my new Mac OS X machine. I have hand-hacked a few of the smaller ones to make sure they work under my Apache/PHP combo. They do!

However, the show-stopper is that most of my BBedit files have MAC newline characters... this displays without problem with TextEdit and BBedit. Even when I edit the files in Pico, the newlines are treated as newlines. The whole system handles mac newlines nicely.

Except, however, the PHP interpreter can't seem to handle them, and always kicks up an error on "line 3" (actually really something like 103, because the mac newlines aren't counted as real lines).

A long time ago, I saw a *very short* PERL script that could recursively traverse a directory tree, and swap characters as needed. I have it in mind to swap all ^M characters for \n, for all files and directories in Library/WebServer/Documents/*.php*.

This would be so much easier than manually changing newlines in BBedit every time a script throws up an error. Some of my projects have zillions of files.

Anybody have this magic script? This *has* to have come up already.
# convert windows cr/lf
$mybadtext =~ s/\r\n/\n/sg;

# convert mac cr
$mybadtext =~ s/\r/\n/sg;

Set all your editors to use Unix line-feeds by default. Take off and nuke them from space. It's the only way to be sure.
cpan and I are not on speaking terms, and there is a dearth of easy-to-find scripts even with a google search "perl recursive subdirectory".

If you're interested in this topic, the following page has a lot of useful code snippets:

Looking at the samples, I remember both why I loved Perl and why I don't just crack a reference manual and start looking things up.

I'll see if I can't hack a recursive dir traverser and newline swapper (thanks slur), and if I can I'll post it here for the world.
If you download the attached script, rename it to You can run it from commandline with:

% perl

It gives a short description, and then asks you some questions. Then it recursively searches the directory you specified (starting from the script's directory) for files matching the extension you specified. It changes Mac newlines into Unix linefeeds and makes a backup file with the extension you specified.

I ONLY use it to migrate my old mac code. If you don't know what you're doing, this script can truly fsck up your machine. Use at your own risk, I accept no liability.

I've also included a few comments so people know what to hack (e.g., if you're searching and replacing something aside from newlines).


  • macnewlines.txt
    2.2 KB · Views: 27
Just ran this guy again on a new project, and I had the script searching for files ending in php. I also had a /php subdirectory; the script choked and died gracefully.
NOTE: script also ignores directories if they have a space in their name. I find this useful for when I want the script to skip certain directories.
a: bbedit wasn't out for X when I needed to change zillions of files' newlines. Before that I had BBedit 5.1 which was giving me headaches under classic. Pepper doesn't launch for me, and Jedit confuses my BBedit sensibilities (though it's a great program!!).

b.1: bbedit allows recursive filtered dir searches
b.2: bbedit prefs let you specify to use Unix linefeeds all the time
b.3: there's a way to search/replace newlines via bbedit's search dialog, but you'll have to rtfm about how to refer to mac newlines and unix linefeeds (if it's even an issue), so you can search for the former and replace with the latter. I'm sure it's possible, but I'm not going to sweat it now.

c: For this thread, I only needed a specific tool (e.g., a green claw hammer with rubber handles for a leftie). So in true Unix geek fashion I made a green claw hammer with rubber handles for a leftie. It only took a little while, and now I have a commandline tool to do a pretty cool specific thing. I've used it <15 times.

d: Change one line of the perl script, and you're searching/replacing any regexp pattern you want, recursing a dir structure, with backups made automatically.

Granted, the perl approach is not for everyone (witness the <3 downloads). :) In some instances, 1 line of text on a commandline is better than launching a program, a bunch of keystrokes, and a bunch of clicks.
Not when you have the file visible in the finder, then it's a really simple drag+drop operation plus I tend to do a lot more within the BBEdit interface.

What would be really cool is if I could drag a file from the Terminal after typing a command to list. If we had the source to we may be able to make a customized tcsh which would output file links which would format like a normal color ls listing, but the files would be files which could be dragged. Also directories (which are typially in blue) could recieve drags.

Damn that would be cool.
That IS a cool idea.

[a] drag-n-drop: drag files off telnet window to the finder

macros -- define commands or file locations resident *in the console* (not symlinks, aliases, etc.) for quick access

[c] point-and-click -- option-click files and select "Edit" from the popup menu to open them, or "Run" to run an app

[d] colors &etc. -- all the stuff normal terminal windows can do.

If done right, you could drag a file into the window, hold it there, have a pop-up window appear (like the click-wait of OS 9), select a pre-defined macro, and have the file location passed to the macro as an argument, with the macro's output going to the terminal window's current directory.

It would be a fundamentally SLUI approach, but with GUI features. The the man page viewer XmanView displays man pages as html. The functionality would be in addition to the normal considerable shell functionality, independent of shell.
It can be done using tv100 codes. I'm not an expert but at the very least specific color codes can be reserved and used as tags. like:

•filename•/location/of/filename• would then interpret this to display a link titled filename. It does something pretty similar when displaying colored text.