06. Applying sed with multiple files and commands

Now let's learn how to perform two more feats - applying a sed command to multiple files, and applying multiple commands to a single file.

Invoking multiple sed commands

You'll most likely want to substitute a number of times - not just once. We can do this in a naive manner with pipes.

$ sed 's/OS/ring/g' oneOS.txt | sed 's/Linux/Mordor/g' | sed 's/hackers/orcs/g'
One ring to rule them all, One ring to find them. One ring to call them all, And in salvation bind them. In the bright land of Mordor, Where the orcs play.

This works, but there is an unnecessary amount of overhead, and uses multiple processes, making it extremely inefficient. Instead, we can use the -e option to string these substitutions together.

$ sed -e 's/OS/ring/g' -e 's/Linux/Mordor/g' -e 's/hackers/orcs/g' oneOS.txt
One ring to rule them all, One ring to find them. One ring to call them all, And in salvation bind them. In the bright land of Mordor, Where the orcs play.

We may also use the semi-colon character (;) instead.

$ sed 's/OS/ring/g;s/Linux/Mordor/g;s/hackers/orcs/g' oneOS.txt
One ring to rule them all, One ring to find them. One ring to call them all, And in salvation bind them. In the bright land of Mordor, Where the orcs play.

Applying sed scripts

Oftentimes at your workstation, you'll find that you're performing the same operations for standardized formatting with sed. We could apply them all at once them with the -e or semi-colons (;) as shown before, but this can get messy and the whole process is not as portable.

A better way to perform a list of text manipulations is through a sed script. Here, we take each sed command and place them line-by-line in a single file. To run the file, we supply it as an argument to the sed command.

# mySedScript
s/rule/cure/g
s/OS/antibiotic/g
s/hackers/doctors/g

Now we call it:

$ sed -f mySedScript oneOS.txt
One antibiotic to cure them all, One antibiotic to find them. One antibiotic to call them all, And in salvation bind them. In the bright land of Linux, Where the doctors play.

Take your Linux skills to the next level!

How Linux Works

Take your Linux skills to the next level! Try Linux & UNIX

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Ad