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";
$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 = \
\$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
= \$_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
*.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…