CLI Magic: Who Sed that?

134

Author: Joe Barr

Wake up, you lolly-gagging Gui-gophers, or you’ll miss what’s being Sed. If you haven’t used Sed before, you’re missing a powerful jewel. It’s a fast and useful tool. First, a confession. Before writing this column I had never used it. I wasn’t even sure what it did. Now, I’m an expert. Come on, I’ll tell you all about it.So what is Sed? I could lie and say it’s an editor for Field and Stream, but that would only be partially true. It’s the GNU Stream Editor. That is, the data to be edited is passed by the editor instead of the other way around. One time. Top to bottom. Start to finish. Get it? Sed acts on the file once, as it passes by.

Let’s use the following old paragraph of text for our examples throughout the column. Here it is as we found it in a dusty old archive.

For release December 15, 1993. Thinking about moving off of Megasloth’s DOS to the potentially unstable, unauthorized, substitute DOS from Novell? Don’t do it! Our latest version will be released soon! Independent studies have confirmed that the next version of Megasloth is cheaper, better, faster, and more secure than ever before! Not only that, but if you pre-purchase the next release, we’ll have your name taken off the BSA’s roll of potential victims. So instead of switching to some buggy, quirky platform, just sit tight and wait for our next release, called the Dynamic DOS Rimshot. Until then, sit tight, keep patching, keep praying, and keep sending us all of your money.

There. Save a copy of that text as a file named text-01.txt. Now we can start learning Sed.
The first thing we need to do is to bring the sample text forward in time a bit. We can do that by telling Sed to substitute 1995 for 1993 in the original text. To do so, enter the following command at the CLI:


sed -e 's/1993/1995/g' text-01.txt

Which produces the following:

For release December 15, 1995. Thinking about moving off of Megasloth’s DOS to the potentially unstable, unauthorized, substitute DOS from Novell? Don’t do it! Our latest version will be released soon! Independent studies have confirmed that the next version of Megasloth is cheaper, better, faster, and more secure than ever before! Not only that, but if you pre-purchase the next release, we’ll have your name taken off the BSA’s roll of potential victims. So instead of switching to some buggy, quirky platform, just sit tight and wait for our next release, called the Dynamic DOS Rimshot. Until then, sit tight, keep patching, keep praying, and keep sending us all of your money.

We’ve still got a few substitutions left to do, and that command line is going to get too long real quick. Let’s open a new file called sed.cmd to keep all our commands in. Sed is perfectly happy with such a script.

Put all of these commands in your sed.cmd-1 file:


s/1993/1995/
s/substitute DOS from Novell/fat and slow OS2 from IBM/
s/Dynamic DOS Rimshot/Dynamic GUI Desktop/

Now tell Sed to work its magic using the script we just created as its input. To do so, replace the -e in the command line — which indicates an expression follows — with -f — which indicates a file name follows.


sed -f sed.cmd-1 text-01.txt

And our text now looks like this:

For release December 15, 1995. Thinking about moving off of Megasloth’s DOS to the potentially unstable, unauthorized, fat and slow OS2 from IBM? Don’t do it! Our latest version will be released soon! Independent studies have confirmed that the next version of Megasloth is cheaper, better, faster, and more secure than ever before! Not only that, but if you pre-purchase the next release, we’ll have your name taken off the BSA’s roll of potential victims. So instead of switching to some buggy, quirky platform, just sit tight and wait for our next release, called the Dynamic GUI Desk. Until then, sit tight, keep patching, keep praying, and keep sending us all of your money.

That version is certainly more current, but it’s still not up to the moment. Let’s revise our command file, using these commands:


s/December/January/
s/1993/2005/
s/substitute DOS from Novell/communist-inspired Linux IP pirates/
s/Dynamic DOS Rimshot/Dynamic Longshot/

Which produces:

For release January 15, 2005. Thinking about moving off of Megasloth’s DOS to the potentially unstable, unauthorized, communist-inspired Linux IP pirates? Don’t do it! Our latest version will be released soon! Independent studies have confirmed that the next version of Megasloth is cheaper, better, faster, and more secure than ever before! Not only that, but if you pre-purchase the next release, we’ll have your name taken off the BSA’s roll of potential victims. So instead of switching to some buggy, quirky platform, just sit tight and wait for our next release, called the Dynamic Longshot. Until then, sit tight, keep patching, keep praying, and keep sending us all of your money.

If we’re happy with the results, it would be a good idea to save the output from Sed, which we haven’t been doing. Just add -i immediately in front of the input text file name to save the results in place, or add > newfile.txt to tell Sed to put its output in whatever you decide the name should be.

Sed is capable of quite a bit more heavy-lifting than simple substitution. Dig as deep as you like. Sed’s man pages can be brutally incomprehensible if you’re not already familiar with all sorts of Unix geekology. There are several excellent starting places which I found more helpful. One is the series of tutorials that Daniel Robbins did for IBM. Google for others.