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!

Linux for Beginners

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

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Ad