Solarian Programmer

My programming ramblings

Starting with GNU sed on Mac OSX

Posted on March 7, 2012 by Paul

If you are a hard-core Linux user who just bought a shiny new Mac machine, you are going to have a big surprise realizing that not every command line skill you acquired over the years will be directly applicable in a Mac Terminal. Take for example the humble and useful sed editor, invaluable when you need to modify on the fly a bunch of text files. You probably assume you could just fire up a Terminal and get your work done. Well it is not so simple, Mac OSX has his roots in Unix just like Linux, but it has a different flavour of command line utilities, a BSD one. On the other hand Linux uses the GNU version of these command line tools. Small but subtle differences have accumulated over the years.

Let’s try a small experiment, I have a folder filled with html files in which I want to change every occurrence of a website address. In Linux I could write this in a console:

1 sed -i 's/old-address/new-address/g' *.html

What I’m trying to do is to change https://solarianprogrammer.com/wp-content/uploads into /images. Look what my Mac has to say about this:

1 sol:~ sed -i 's/https:\/\/solarianprogrammer.com\/wp-content\/uploads/\/images/g' *.html
2 sed: 1: "tst.html": undefined label 'st.html'
3 sol:~

Even if Apple’s sed is not happy with it, the above is a perfectly valid GNU sed command.

You have two solutions: first RTFM which in a polite form means read the man page, or you could cheat and install GNU sed from sources. There is obviously a third solution to write a small and portable script in your preferred poison: Python, Ruby, Perl - all of them already installed on your machine. Not mentioning that you could write a Bash script if you feel inclined. I’ll take the path of minimum effort and compile GNU sed from source. In the next part of this post I will assume that you have already installed Xcode on your system. If you use Xcode 4.3 and up, please note that Apple has removed the CLI tools from the main installer, you could however install them from Xcode.

First you need to download the last stable version of sed, currently 4.2, from ftp://ftp.gnu.org/gnu/sed/ and extract this on your machine, cd to the extracted directory and configure the package. Since we don’t want to mess with Apple’s default sed, we’ll chose to add a suffix to GNU’s version, for convenience we’ll install sed in /usr:

1 mkdir build && cd build
2 ../configure --prefix=/usr/sed-4.2 --program-suffix=-4.2

If everything goes well and you see no error message, we can compile and install sed:

1 make -j 8
2 sudo make install

In order to be able to use GNU sed we’ll need to add it to our shell’s path with:

1 export PATH=/usr/sed-4.2/bin:$PATH

If you don’t want to write/paste the above line each time you want to have sed available, you could add it at the end of your .bashrc file, after that GNU sed will be always at your finger tips. Let’s try again to modify our bunch of html files:

1 sed-4.2 -i 's/https:\/\/solarianprogrammer.com\/wp-content\/uploads/\/images/g' *.html

If you want to learn more about using sed, there is a good book sed & awk by Dale Dougherty and Arnold Robbins:


Show Comments