sed replace examples

sed replace

sed is one of the advanced Unix commands for search/replace processing of text information. Command itself is very simple but most scenarios requiring its use are fairly advanced elements of systems administration. Thanks to Unix pipes, sed is often used chained with other commands to transform fragments of text.

Basic sed replace example

Here’s how you can replace one of the words in an text given as input to sed:

greys@maverick:~ $ echo 'hi, world'
hi, world
greys@maverick:~ $ echo 'hi, world' | sed 's/hi/hello/'
hello, world

sed Replaces Every Matched Occurrence

It’s not always obvious from a single line example, but sed processes all the lines of the given text input and makes as many replacements of matched patters as necessary:

greys@maverick:~ $ echo -e 'hi, world \n hi again' | sed 's/hi/hello/'
hello, world
hello again

sed is often used together with awk command, together they are a very powerful combination for processing large amounts of text and converting text into the desired format.

See Also