Table of Contents

Sed Find And Replace With Examples

Last updated: Sep21st, 2020

In this post, we will go over 'how to use sed to replace text'. Also checkout my short tutorial on replace white text using sed and awk.

Let us create a dummy text file example.txt with text shown below. Note we have also added two empty lines.

echo "sed is a great utility for file processing in Linux." > example.txt
echo "" >> example.txt
echo "sed can be used to replace text in a file. sed can also replace text globally and selectively in a file." >> example.txt
echo "sed is a command line utility in Linux which replaces text." >> example.txt
echo "" >> example.txt
echo "sed can be used to print the content of file and replace text as well in file" >> example.txt

Sed replace nth occurrence of word or string

Replace 2nd occurrence of word file with doc. So we should see in line number 3 and 6, 2nd occurrence of word 'file' will be replaced with 'doc'.

sed 's/file/doc/2' example.txt
> sed is a great utility for file processing in Linux.

> sed can be used to replace text in a file. sed can also replace text globally and selectively in a doc.
> sed is a command line utility in Linux which replaces text.

> sed can be used to print the content of file and replace text as well in doc

Sed replace every occurrence of pattern or string with global option g

sed replaces every occurrence of specific word. Below command will replace all occurrences of word 'file' with word 'doc'.

sed 's/file/doc/g' example.txt
> sed is a great utility for doc processing in Linux.

> sed can be used to replace text in a doc. sed can also replace text globally and selectively in a doc.
> sed is a command line utility in Linux which replaces text.

> sed can be used to print the content of doc and replace text as well in doc

Below command will replace every occurrence of word "file" with word "doc" starting from 3rd line and onwards.

sed 's/file/doc/3g' example.txt
> sed is a great utility for file processing in Linux.

> sed can be used to replace text in a file. sed can also replace text globally and selectively in a file.
> sed is a command line utility in Linux which replaces text.

> sed can be used to print the content of file and replace text as well in file

Sed replace pattern or string on nth line

Replace pattern on specific line number Below sed command will replace word "file" only on line number 3.

sed '3 s/file/doc/g' example.txt
> sed is a great utility for file processing in Linux.

> sed can be used to replace text in a doc. sed can also replace text globally and selectively in a doc.
> sed is a command line utility in Linux which replaces text.

> sed can be used to print the content of file and replace text as well in file

Sed replace with -n and p option

Let us rerun the above command but this time we will print only line number 3. For that we need to use option 'p'. p is for print. If we use p, p will print every line twice. To make sure we print only the line which includes pattern, we have to use with switch -n in the beginning as shown below.

sed -n '3 s/file/doc/gp' example.txt
> sed can be used to replace text in a doc. sed can also replace text globally and selectively in a doc.

As we see above command, only prints line number 3. Also note word 'file' has been replaced with word doc in above line.

Sed replace pattern2 based on another pattern1

Below sed command will only replace "word" file with "doc" in line where the pattern "selectively" is found which is line number 3.

sed -n '/selectively/ s/file/doc/gp' example.txt
> sed can be used to replace text in a doc. sed can also replace text globally and selectively in a doc.

Below command will look for lines starting from line with a pattern "selectively" (line 3) till the lines with pattern "content" (last line) and then replace pattern "file" with "doc"

sed -n '/selectively/,/content/ s/file/doc/gp' example.txt
> sed can be used to replace text in a doc. sed can also replace text globally and selectively in a doc.
> sed can be used to print the content of doc and replace text as well in doc

Sed replace pattern from piped (another command) output

Note: Instead of providing file, sed can also read from the standard input. We can pipe the output to sed. Let us run the above command using pipe.

In following command we are passing the out of cat to sed. The below command has the same affect as we saw above.

cat example.txt | sed -n '/selectively/ s/file/doc/gp'
> sed can be used to replace text in a doc. sed can also replace text globally and selectively in a doc.

Sed replace a string with additional characters

sed can replace a string with additional characters. Let us say we want to add round () brackets around each word in line 3. In the following command, we are using ampersand (&) character to save each word.

sed -n '3 s/[a-z]\+/(&)/gp' example.txt
> (sed) (can) (be) (used) (to) (replace) (text) (in) (a) (file). (sed) (can) (also) (replace) (text) (globally) (and) (selectively) (in) (a) (file).

Sed replace or remove empty lines in a file

To remove empty lines, we can use ^\s$ regular expression. ^ means start of line. \s means space. \s(star) means zero or more spaces. $ means end of line. Therefore above expression will remove all the lines which start with space and end with spaces.

sed '/^\s*$/d' example.txt
> sed is a great utility for file processing in Linux.
> sed can be used to replace text in a file. sed can also replace text globally and selectively in a file.
> sed is a command line utility in Linux which replaces text.
> sed can be used to print the content of file and replace text as well in file

Sed find pattern and insert/append new line after a line with matched pattern using option a

To append a line use option 'a' as shown below. In the below command, we are adding a new line with text 'adding new line' after the line with pattern/word 'utitlity'. Therefore we would see two lines added, one after line 1 and another after line 4.

sed '/utility/a adding new line' example.txt
> sed is a great utility for file processing in Linux.
> adding new line

> sed can be used to replace text in a file. sed can also replace text globally and selectively in a file.
> sed is a command line utility in Linux which replaces text.
> adding new line

> sed can be used to print the content of file and replace text as well in file

Sed multiple replace commands with switch -e

In sed instead of pipe, we can use switch -e to concatenate multiple sed commands. For this example let us concatenate previous two commands using switch -e

sed -e '/utility/a newline' -e '/^\s*$/d' example.txt
> sed is a great utility for file processing in Linux.
> newline
> sed can be used to replace text in a file. sed can also replace text globally and selectively in a file.
> sed is a command line utility in Linux which replaces text.
> newline
> sed can be used to print the content of file and replace text as well in file

Sed find and insert/append new line before a line with matched pattern using option i

sed '/utility/i adding new line' example.txt
> adding new line
> sed is a great utility for file processing in Linux.

> sed can be used to replace text in a file. sed can also replace text globally and selectively in a file.
> adding new line
> sed is a command line utility in Linux which replaces text.

> sed can be used to print the content of file and replace text as well in file

Sed replace a line with new line using option c

sed '/utility/c adding new line' example.txt
> adding new line

> sed can be used to replace text in a file. sed can also replace text globally and selectively in a file.
> adding new line

> sed can be used to print the content of file and replace text as well in file

Related Posts