Sed on Mac OS X 10.5 Leopard

Imagine you’re a PHP developer that uses OS X. You’re given 50+ php files that all have a line that needs to be changed. 50 files, one change? Hmmm sounds like maybe I could use automator to do this! Well I don’t know how to use automator 😉 Luckily, I’m a unix geek, and even more luckily, OS X has a fairly strong unix back end and a great terminal emulator. So how does that help? Well if you’re a unix geek you know that this sort of problem just screams “SED! use SED! this is what SED IS FOR!” And it’s true. This is where sed is great.

Say you want to change this line:

 $config_file = $_SERVER['DOCUMENT_ROOT']."/dev/config.php";

To this:

 $config_file = "dev/config.php";

As a matter of fact… wait a minute! I don’t want to just change that line… no I want to be able to show what I’ve changed, so that the next person who looks at this can see what it used to say. In this instance it’s also important to show the work I’ve done because I’m making changes to someone else’s work. So what I want to do is comment out the line and then add a new line with my change underneath. Sounds a bit more complicated right? Well it’s not really – unless you’re using OS X (you’ll see why in a minute *sigh*). The goal is to end up with this:

 //$config_file = $_SERVER['DOCUMENT_ROOT']."/dev/config.php";
 $config_file = "dev/config.php";

On the linux machine I keep around the office the command to perform this change on the all php files in the current directory looks like this ( I had to split the line to fit on the site):

sed -i .bak "s/^\$config_file = \
\$_SERVER\['DOCUMENT_ROOT'\]\.\"\/dev\/config\.php\"\;/\/\/\
\$config_file = \$_SERVER\['DOCUMENT_ROOT'\]\.\"\/dev\/config\.php\"\;\
\n\$config_file = \"dev\/config\.php\"\;/g" *.php

The breakdown of the above command is as follows:

sed | the sed command

-i .bak | the -i option means do this change “in place” and copy the original file to originalname.bak

| we use the double quote here to start the sed command because there are single quotes in the command

s/^\$config_file =
\$_SERVER\[‘DOCUMENT_ROOT’\]\.\”\/dev\/config\.php\”\;/\/\/\$config_file
= \$_SERVER\[‘DOCUMENT_ROOT’\]\.\”\/dev\/config\.php\”\;\n\$config_file = \”dev\/config\.php\”\;/g
| this is the magic of sed. it looks for the line we want to change, adds // to the beginning of it, adds a new line after it, and then adds the text that we want after the new line. Amazing isn’t it?

| the command ends with the double quote

and, finally:

*.php | represents all of the files that end in .php in the current directory.

Now if you thought that was a mess, check out how that command needs to look in order to work on OS X (10.5.1, possibly other versions)

sed -i .bak "s/^\$config_file = \
\$_SERVER\['DOCUMENT_ROOT'\]\.\"\/dev\/config\.php\"\;/\/\/\
\$config_file = \$_SERVER\['DOCUMENT_ROOT'\]\.\"\/dev\/config\.php\"\;\
\\"$'\n'"\
\$config_file = \"dev\/config\.php\"\;/g" index.php

The crazy part here is the need to use \\”$’\n'”\

What that represents is an escaped version of the literal newline character. With the version of sed currently in OS X, that is the only way (that I could find) to add a newline character with sed. Now you know too.

So that concludes this edition of mac unix geekery… until next time…

Mysql 5 client from MacPorts and Server from Official Package

I just noticed that when you use the mysql 5 client from MacPorts (installed as /opt/local/bin/mysql5) to connect to a mysql5 server running on localhost that was installed via the package at dev.mysql.com, an error is generated:

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’ (2)

This is because the installation of mysql 5 server from the mysql site uses /tmp to hold the socket file. Of course if you read the README you’d know this:

“The installation layout is similar to that of a `tar’ file binary
distribution; all MySQL binaries are located in the directory
`/usr/local/mysql/bin’. The MySQL socket file is created as
`/tmp/mysql.sock’ by default.”

I, of course, did not read the README first, and so I wondered what I was doing wrong. The fix is easy, just add

-S /tmp/mysql.sock

to your mysql5 command to use it without any configuration (i.e. changing the location of the socket).

Dubwar!

Yes tonight is the night! DUBWAR! I’ve been waiting just about forever to see D1 and thanks to DQ and the dubwar crew it’s on!! You can get tickets at http://musicislove.net
be there!

Check out the Dubwar Homepage

P.S. – IT WAS INCREDIBLE! If you weren’t there then I’m sorry to say you missed out on one of the best jams ever. Seriously it was that good. Massive thanks to DQ and the Dubwar family. Shout out to DJ Shiva and the Midwest Dubstep crew – Got your podcast downloaded. Word. Dubstep in the USA.

More news! – Check out the press! Dubwar in the NY Times

Music

“Music. I’m so in love with my music. The way you keepin’ movin… Ain’t nobody do what you doin…”

Joss Stone is so awesome. Love that woman!! You know there is a divine power when you see a beautiful woman making beautiful music. How can you deny it? I just can’t be an atheist when I listen to Joss. Why? Because all I can think is… “God! She is so amazing!” Who’s with me on this? I know Larry hears me but he probably won’t read this unless I ask him to (hi Larry).

Could it all be luck? A random sequence of molecules? What made Me and Joss and Music? I suppose the origin isn’t as important as the fact that all of the above exists, right? Well if you hear me… God, Goddess, chaos… Thanks! (for music and women, among other things…)

P.S. – check out http://www.jossstone.co.uk/ (it’s better than the non-uk home page, I think)

P.P.S – here’s Joss Stone (Hi Res)

Robeson

Ever heard of Paul Robeson?

I was listening to one of my favorite artists, Saul Willams, the other day – when it occurred to me that he speaks about some historical figures that I didn’t know much about.  One of these is Paul Robeson. Saul Williams has a song entitled Robeson, and also mentions the name in “Coded Language” which is on the same album (Amethyst Rockstar). I really enjoy learning, and Saul Williams is an artist whose lyrics have a lot to offer in terms of knowledge, so I took some time to do a little research.

It turns out that Paul Robeson’s life is really interesting. While wikipedia is generally not to be considered an absolute source of facts, the entry regarding Paul Robeson is more than enlightening. I’m not going to quote the page here, since you can just read it yourself, but I will say that if you want to read about an interesting figure in American history, particularly if you are interesting in African American history, it’s well worth the time to read. Also, with regards to wikipedia as a source of facts, this entry is chock full of real references and cited sources.