How to Use the Linux sed Command in 2024 With Useful Examples and Use Cases

How to Use the Linux sed Command in 2024 With Useful Examples and Use Cases

The Linux sed command lets you find, replace, insert, and delete lines in a file without opening it using a text editor. Suitable for piped input and various file formats, this Linux command also uses regular expression or regex, allowing you to search and manipulate complex patterns.

With its various use cases, understanding how to use sed can help system administrators efficiently manage their virtual private servers (VPSs).

This article will explain the Linux sed command’s general syntax, options, and subcommands. You will also learn various sed examples and use cases for managing Linux VPS.

sed Command Options

Here’s what the sed command’s general syntax looks like:

sed options 'script' file_name

To modify sed commands’ behavior, add the following command-line options:

  • –help – prints command usage information.
  • –debug – enables Terminal to annotate program execution and input.
  • -i – overwrites the original file.
  • -n – disables automatic printing unless the user uses the p command.
  • -u – minimizes output.
  • –posix – disables POSIX sed extensions to simplify writing portable scripts.
  • -e – specifies multiple commands to run sequentially.
  • -b – opens input files in binary mode.
  • -l – sets the desired line-wrap length for the l command.

The script contains the subcommand, search pattern, replacement string, and flag. These elements are encapsulated in apostrophes and separated using a delimiter, like a slash (/), backslash (\), or pipe (|).

Their order may differ depending on the subcommand. For example, the s or substitute command replaces a regular expression pattern with another string. Here’s the syntax:

's/regex_pattern/new_pattern/flags'

To alter the pattern substitution, use the following flags:

  • g – applies global replacement, not just the first occurrence.
  • Number – specifies which line numbers to modify.
  • p – prints the new line after a successful pattern replacement.
  • i – makes the substitution case sensitive.

Pro Tip

The s subcommand supports multiple flags. For example, add the gi flag to enable a global, case-sensitive substitution.

How to Install sed?

The Linux stream editor package comes pre-installed in most distributions. If your system doesn’t have the tool, follow these steps to install it:

Important! In this tutorial, our VPS is running Ubuntu 20.04. If you use another distribution or version, the commands may differ.

  1. Connect to your VPS using an SSH client like PuTTY or Terminal. Hostinger users can use the Browser terminal via hPanel.
  2. Enter your root login credentials. On hPanel, they are located in the VPS overview menu’s SSH access tab.
  3. Update the repository by typing this command:
sudo apt-get update
  1. Install the sed package by entering the following command:
sudo apt-get install sed
  1. Run the command below to check whether the installation was successful:
sed --version
Terminal shows the currently installed sed version

sed Command Examples

In the following sections, we will present 10 stream editor command examples to help you understand its functions.

Important! All the commands below don’t alter the original file. To apply the changes directly, add the -i option.

Using sed to Search and Replace a String

The sed command is commonly used for replacing text. This tool will search for the specified pattern in a file and change it with the desired string.

To do so, use the s command with the string you want to replace and its replacement. Here’s the syntax:

sed 's/old_string/new_string/' samplefile.txt

Change the placeholders with the actual value. For example, this command substitutes the word “images” with “photos” in the scenery.txt file:

sed 's/images/photos/' scenery.txt
Terminal output shows sed has replaced the word images with photos

If your string contains the slash symbol, use another delimiter, like a backslash (\) or pipe (|).

Using sed to Replace the nth Occurrence of a Pattern in a Line

If a pattern in a line occurs multiple times, enter the following command syntax to replace a specific one:

sed 's/old_string/new_string/#' samplefile.txt

Substitute the hash (#) symbol with the pattern’s sequence number. For example, this command replaces the first occurrence of the word “music” with “song” in a line inside the playlist.txt file:

sed 's/music/song/1' playlist.txt
Terminal output shows sed has replaced the first music occurrence with song

Using sed to Replace All the Occurrences of the Pattern in a Line

By default, the sed command replaces only the first instance of the specified string and moves to the next input line. To replace all matching patterns in the same line, add the g flag. Here’s how the sed script looks:

sed 's/old_string/new_string/g' samplefile.txt

For example, run the following to replace all occurrences that contain “eagle” with “falcon” in a line inside animals.txt:

sed 's/eagle/falcon/g' animals.txt
Terminal output shows sed has replaced all eagle occurrences with falcon

Using sed to Replace an Occurrence From nth to All Occurrences in a Line

Instead of replacing all patterns within the same line, combine the number and g flag to replace occurrences starting from a specific one. Here’s the sed script:

sed 's/old_string/new_string/#g' samplefile.txt

For example, the command below replaces the word “pisces” with “aquarius” from the second occurrence until the last one in the astrology.txt file.

sed 's/pisces/aquarius/2g' astrology.txt
Terminal output shows sed has replaced the second pisces occurrence until the last one with aquarius

Using sed to Parenthesize the First Character of Each Word

To print the first character of every word in parenthesis, use the following script:

echo "desired_sentence" | sed -E 's/(\b\w)/(\1)/g'

For example, to display the first character of “An example of the sed command” in parenthesis, enter:

echo "An example of the sed command" | sed -E 's/(\b\w)/(\1)/g'
Sed parenthesizes each word's first character of the piped sentence

To parenthesize each word’s first character from a file, omit the echo command and add the input document at the end.

Using sed to Replace the String on a Specific Line Number

To replace the string on an nth line, add its sequence number before s like this syntax:

sed '#s/old_string/new_string/' samplefile.txt

For example, enter the following to substitute the word “cake” with “bread” in the second line of foods.txt:

sed '2s/cake/bread/' foods.txt
Terminal output shows sed has replaced cake in the second line with bread

Using sed to Duplicate the Replaced Line With the /p Flag

To print lines that your sed command modified as an additional output, use the p or print flag. Here’s the general syntax:

sed 's/old_string/new_string/p' samplefile.txt

For example, run the following to replace “phones” with “tablets” in the gadgets.txt file and print the results:

sed 's/phones/tablets/p' gadgets.txt
Terminal prints the replaced line as an additional output

Terminal will print the original string if the line doesn’t contain the search pattern and is not substituted.

Using sed to Replace the String of a Range of Lines

The sed command lets you modify only the line numbers specified in the script by adding the range. Here’s the syntax:

sed '#,# s/old_string/new_string/' samplefile.txt

For example, the command below replaces “germany” located in the third, fourth, and fifth line on the countries.txt file with “france”:

sed '3,5 s/germany/france/' countries.txt
Terminal output shows sed has replaced germany on the third to fifth line with france

Using sed to Print Only the Replaced Lines

By default, the stream editor prints the entire file content. To simplify the output, combine the -n option with the p command to show only the matching lines. Here’s the general syntax:

sed -n 's/old_string/new_string/p' samplefile.txt

For example, to replace the third instance of “green” with “blue in a line inside the colors.txt file and print the modified lines on the terminal window, enter:

sed -n 's/green/blue/3p' colors.txt
Terminal prints a single line that sed modified

Using sed to Delete Lines From a Particular File

The d or delete command lets you remove lines from a file without a text editor. For example, use the following syntax to remove a particular line number:

sed '#d' samplefile.txt

Replace the hash (#) symbol with the line number you want to delete. For example, run this command to remove the first line from the cities.txt file:

sed '1d' cities.txt

In addition, you can delete all the lines within a specific range using the sed command:

sed '#,#d' samplefile.txt

Replace the hash (#) symbols with the starting and ending line numbers. For example, enter the following to delete the first to the third line in the cars.txt file:

sed '1,3d' cars.txt

You can also delete the last line in a file by combining the d subcommand and a dollar sign ($), like the following.

sed '$d' samplefile.txt

To delete a specific line number starting from the last one, use the following syntax:

sed '#,$d' samplefile.txt

For example, this command will remove the second to last line in the books.txt file:

sed '2,$d' books.txt

In addition to deleting lines, use this command to remove a particular occurrence in a file. To do so, specify the regex pattern in your script, like the following syntax:

sed '/pattern/d' samplefile.txt

For example, run this to remove the “oabo” pattern from the filestrings.txt file:

sed '/oabo/d' filestrings.txt

sed Command Use Cases

In this section, we will explain how to use sed commands for different use cases in server management.

Use sed for Batch Processing of Files

Generally, there are two ways to edit files in bulk using the sed command.

First, specify the files individually. With this method, you will list all the input files you want to replace at the end of your command, separated using spaces. Here’s the syntax:

sed 's/old_string/new_string/g' filename1.txt filename2.txt

The command will simultaneously find and replace all old_string occurrences in the two text files.

Second, scan them using the find command. This method automatically searches for files containing the specified pattern in a directory. Here’s the syntax:

find /directory/path/file -type f -exec sed -i 's/old_string/new_string/g' {} \;

Replace /directory/path/file with the directory containing the files you want to process.

When using the second method, create a backup file for easy restoration in case of accidental substitutions. Alternatively, omit the -i option to disable in-place editing and keep the original file unaltered.

Use sed to Log File Analysis

In addition to modifying patterns, the sed command in Linux is also useful for log analysis. It lets you easily search for a specific pattern in the log file and extract the results for easier diagnostics.

To do so, add the > symbol to output matching patterns into a text file. While the command differs depending on your usage, the basic syntax remains as follows:

sed -n 's/pattern/p' logfile.log > extracted_data.txt

Change the regex pattern according to the data you want to search for, like IP address, error message, or timestamp. For example, use this script to export logged errors:

sed -n 's/Error: \(.*\)/\1/p' logfile.log > error_logs.txt

Use sed for HTML/XML Tag Manipulation

The sed command in Linux lets you easily replace strings in code. For example, you can search for specific HTML or XML tags and replace their attributes using the substitution command. Here’s the syntax:

sed 's/<tag attribute="old_pattern">/<tag attribute="new_pattern"/' file.html

The command varies depending on the tags and attributes you want to change. For example, this sed command changes all heading tags’ color attributes to black:

sed 's/\(<h[1-6].*color:\) [^;]*/\1 black/g' webpage.html

The command will search for the following HTML code pattern and replace the color value with the new string:

<h1 style="color: value;">Heading 1</h1>

Use sed for External Files

Complex sed command operations may contain multiple scripts. While you can add them in a single command with the -e option, it is difficult to write and prone to error.

Alternatively, create an external SED file containing multiple scripts. Here’s the step:

  1. Run the nano command to create a new script file and open the text editor. Replace script with your desired file name:
nano script.sed
  1. Write your scripts and ensure each line contains one script without apostrophes, like the following:
s/old_pattern1/new_pattern1/g
/old_pattern2/d
  1. Press Ctrl+X and Y to close the editor and save the file.
  2. Run the scripts by adding the file using the -f option in your command. Here’s what the syntax looks like:
sed -f script.sed destination_file.txt

Use sed for Backreferences in Regex

Backreferences let you refer to previously matched patterns and reuse them in the new string. It eliminates the need to rewrite the regex pattern, simplifying the script.

The pattern used as the sed command reference is called a capture group. Encapsulated in parentheses, it also slashes for extended regular expressions.

To refer to the capture group, use a backslash (\) and a number indicating the pattern order. For instance, the \1 backreference will reuse the first captured regex pattern.

Backreferences are useful for substituting and reordering patterns. For example, this sed command uses them to reorder last and first names:

echo "Doe, John" | sed 's/\(.*\), \(.*\)/\2 \1/'

The two \(.*\) regex patterns capture Doe and John, setting them as the reference. Since the new string starts with the second backreference, the new value will be John Doe.

Sed reorders string with backreferences

Conclusion

The stream editor or sed command in Linux is a tool that lets you find and modify a pattern in a file using regular expressions. Its syntax comprises the sed command, options, the script, and the target file.

sed command-line options modify the tool’s behavior, while the script determines how the tool modifies the matching regex pattern. The script contains a subcommand, flags, and the regex patterns, each separated using a delimiter like a slash (/).

To use the sed command in Linux, open command-line applications like Terminal, an SSH client, or Hostinger’s Browser Terminal. To find and replace a pattern, use the s subcommand. Meanwhile, the d subcommand deletes lines or strings.

The sed command in Linux is also useful for log analysis, modifying markup tags, and processing files in bulk. Moreover, it supports regex backreferences and lets you use a script file to run multiple Linux commands simultaneously.

Linux sed Command FAQ

This section will answer the most frequently asked questions about the sed command.

How Is sed Different From grep?

Both sed and grep are text processing tools in Linux. However, grep doesn’t support text transformation or line manipulation commands like substitution. It is primarily used to find particular text patterns in large files and print the output.

How Is sed Used in the Bash Script?

In the Bash script, the sed command has three primary uses – printing to stdout, deleting a text, and replacing a specific string. The program determines which line it will process from the specified address range.

How Do You Call a Variable in the sed Command?

A variable is a character we assign a value to, providing better readability when added to a sed command. It can be a number, character, file name, or device. The shell expands variables. For example, if the string contains a slash (/), use another delimiter, such as a pipe (|).

How Is the sed Command Different From the awk Command?

The sed command is for basic text modification, like replacing, deleting, and inserting a pattern into an input file. Meanwhile, awk is used for complex tasks like data extraction, text manipulation, and mathematical calculations. It also supports programming statements like if/else and do/while.

Author
The author

Laura Z.

Laura is an experienced Content Production Lead. With 9+ years of experience in marketing under her belt, Laura loves sharing her ideas with the world. In her free time, she enjoys learning about new marketing trends and watching TV shows on Netflix.

Author
The Co-author

Aris Sentika

Aris is a Content Writer specializing in Linux and WordPress development. He has a passion for networking, front-end web development, and server administration. By combining his IT and writing experience, Aris creates content that helps people easily understand complex technical topics to start their online journey. Follow him on LinkedIn.