Substitution with sed

This week, I've been using sed quite a lot. sed is short for stream editor, and is designed to work with large amounts of streaming text.

The most common use case that most people have for sed is text substitution. As it's a stream editor, by default it looks at one line of input at a time.

So for example, given the following input:

the number one is number one
then comes number two
and number three comes third

Running the following command would replace "number" with "NUMBER".

sed 's/number/NUMBER/'

Just like perl, substitution only affects the first match - you need to be explicit to make it replace all occurrences. In this case, that's by adding the g flag.

sed 's/number/NUMBER/g'

So, if we run:

echo "the number one is number one\nthen comes number two\nand number three comes third" | sed 's/number/NUMBER/g'

We should see:

the NUMBER one is NUMBER one
then comes NUMBER two
and NUMBER three comes third

Applying this to an existing set of text can be done by feeding filenames into sed and using the -i flag, saying "change in place".

So for example, to replace all occurrences of the word "Dog" with the word "Cat":

find . -type f | xargs sed -i 's/Dog/Cat/g'
comments powered by Disqus