sed command in Linux Explained [with 20 Examples]

Written by: Nimesha Jinarajadasa   |   Last updated: October 25, 2022

sed, short for "stream editor," is a command-line utility in Linux and Unix-like operating systems used primarily for text manipulation and processing. Its primary usage is to process and transform text using a set of specified commands, often based on regular expressions.

Primary Usage:

  • Commonly used for search-and-replace operations, text filtering, and text transformations in pipelines and scripts.
  • It allows you to specify patterns using regular expressions to perform various text manipulation tasks, such as substitution, deletion, insertion, and more.

Syntax

The basic syntax of the sed command is as follows:

sed OPTIONS 'COMMAND' INPUT_FILE

Here are some common options you can use with sed:

  • -n: Suppress automatic printing of pattern space.
  • -i: Edit files in place (make sure to back up files before using this option).
  • -r: Use extended regular expressions (regex).
  • -e: Specify multiple sed commands.

Let's use the following sample text file as INPUT_FILE:

cat sample.txt
Hello, World!
This is a sample file for sed examples.
Replace the World with Folks! Thanks World!
sed is a powerful tool.

Common sed Commands

Let's look into the sed command with examples.

Printing Lines

Example 1: Print the entire file

sed -n 'p' sample.txt

Output:

Hello, World!
This is a sample file for sed examples.
Replace the World with Folks! Thanks World!
sed is a powerful tool.

Example 2: Print specific line

sed -n '3p' sample.txt

Prints the third line.

Output:

Replace the World with Folks! Thanks World!

Example 3: Print a range of lines

sed -n '2,3p' sample.txt

Prints lines 2 and 3.

Output:

This is a sample file for sed examples.
Replace the World with Folks! Thanks World!

Example 4: Print every other line

sed -n '1~2p' sample.txt

Prints every other line starting from the first line.

Output:

Hello, World!
Replace the World with Folks! Thanks World!

Replacing String

Example 5: Replace a string

sed 's/World/Folks!/' sample.txt

Output:

Hello, Folks!!
This is a sample file for sed examples.
Replace the Folks! with Folks! Thanks World!
sed is a powerful tool.

This replaces the first occurrence of 'World' with 'Folks!' on each line. On the third line, you can see only one occurrence is replaced.

Example 6: Replace all occurrences of a string

sed 's/World/Folks!/g' sample.txt

Output:

Hello, Folks!!
This is a sample file for sed examples.
Replace the Folks! with Folks! Thanks Folks!!
sed is a powerful tool.

This replaces all occurrences of 'World' with 'Folks!' on each line using the g (global) option. You can see the difference in the third line.

To perform a case-insensitive global substitution use s/World/Folks!/gI, where I flag that makes the substitution case insensitive.

Example 7: Replace Nth occurrence of a string

sed 's/World/Folks!/2g' sample.txt

Output:

Hello, World!
This is a sample file for sed examples.
Replace the World with Folks! Thanks Folks!!
sed is a powerful tool.

Replace the second occurrence and beyond of 'World' with 'Folks!' on each line using the 2g option. The output would be the same as above.

Example 8: Replace string on a specific line number

sed '3s/World/Folks!/' sample.txt
Hello, World!
This is a sample file for sed examples.
Replace the Folks! with Folks! Thanks World!
sed is a powerful tool.

This replaces 'World' with 'Folks!' on the third line for the first occurrence.

Example 9: Replace string on a range of lines

sed '2,3s/World/Folks!/' sample.txt

The above command replaces 'World' with 'Folks!' on lines 2 and 3.

Example 10: Replace multiple strings

sed 's/World/Folks!/g; s/Hello/Hi/g' sample.txt

Output:

Hi, Folks!!
This is a sample file for sed examples.
Replace the Folks! with Folks! Thanks Folks!!
sed is a powerful tool.

Replaces 'World' with 'Folks!' and 'Hello' with 'Hi' on each line.

You could also make use of extended regex and the command should look sed 's/\(Hello\|World\)/Hi/g' sample.txt.

Deleting Lines

Example 11: Delete specific lines

sed '3d' sample.txt
Hello, World!
This is a sample file for sed examples.
sed is a powerful tool.

The third line from the file gets deleted.

Example 12: Delete the last line

sed '$d' sample.txt

Here $ sign to denote the last line.

Some more useful examples of deleting lines:

Delete lines from a range: sed '2,3d' sample.txt

Delete lines from a given line number to the last line: sed '2,$d' sample.txt

Delete pattern-matching lines: sed '/World/d' sample.txt

Delete odd lines: sed '1~2d' sample.txt

Remove Character

Example 13: Remove all empty lines

sed '/^$/d' input.txt > output.txt

This removes all empty lines (lines with no content) from a file. In the command /^$/ is a regular expression pattern that matches empty lines. It looks for lines that start ^ and immediately end $ with no characters in between.

Sometimes you need to use /^\s*$/ to match lines that are either truly empty (contain no visible characters) or lines that contain only whitespace characters (spaces, tabs, etc.).

sed example remove all empty lines from file

Example 14: Remove all bank spaces at at beginning of each line

sed 's/^[[:space:]]*//' input.txt > output.txt

This removes all leading blank spaces (whitespace characters at the beginning of each line) from a file.

sed example remove all leading blank space from the file

Example 15: Remove all bank spaces at at end of each line

sed 's/[[:space:]]*$//' input.txt > output.txt

or

sed 's/[[:blank:]]*$//' input.txt > output.txt

Both command removes all trailing blank spaces from a file.

The difference is that the character class [:space:] includes all whitespace characters, including newlines and carriage returns, while [:blank:] specifically represents horizontal whitespace, which includes space and tab characters.

Example 16: Remove all special characters removed from each line

sed 's/[^a-zA-Z0-9 ]//g' input.txt > output.txt

If you want to remove a specific set of special characters, for example, if you want to remove characters such as !, @, #, $, %, etc., you can modify the command like this:

sed -i 's/[!@#$%]//g' file.txt

Your intention is only to remove non-printable characters from a file, then try this:

sed -i 's/[^[:print:]]//g' file.txt

Note: Here i option modifies the original file (file.txt) rather than printing the result to the standard output.

Inserting and Appending Text

Example 17: Insert text before a specific line number

sed '3i This is a new line' input.txt > output.txt

This inserts the text "This is a new line" before line 3 of the input file.

sed example insert a text before specified line

Example 18: Append text after a specific line number

sed '2a This line was appended' input.txt > output.txt

Multiple substitutions

You can perform multiple substitutions in a single sed command by chaining them together using semicolons ;.

Example 19: Using Semicolon Separator (;)

sed 's/old_text_1/new_text_1/g; s/old_text_2/new_text_2/g' input.txt > output.txt

You can also use multiple -e flags followed by substitution commands to perform multiple sequential operations.

Example 20: Using -e Flag

sed -e 's/old_text_1/new_text_1/g' -e 's/old_text_2/new_text_2/g' input.txt > output.txt

About The Author

Nimesha Jinarajadasa

Nimesha Jinarajadasa

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called “03 tiers(DB, M-Tier, and Client)”. Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment