The “sed” Command in Linux [with Practical Examples]

The sed command in Linux stands for stream editor which allows for editing text streams in a file. Though primarily used for text substitution, the “sed” command can perform different operations on file such as insertion, deletion, find and replace, and searching using the stream editor called “sed”. The “sed” command can even edit files without opening them. On top of that, its ability for pattern matching using regular expression makes it a powerful text manipulation tool.

In this tutorial, you’ll learn how to use the sed command in Linux to edit and manipulate files quickly and efficiently.

The Linux “sed” Command Syntax

The sed command is used to edit lines from the File parameter specified in the edit script and write them to the standard output. It allows you to select the lines that you want to edit and make changes only to those lines. It processes files line by line, using regular expressions to search, replace, insert, or delete text based on patterns. This makes it ideal for automating text manipulation operations. However, this modification is temporary. It is only visible on the display, but in reality, the file content remains the same.

The syntax of sed command is as follows:

sed [OPTION]... [COMMAND] [FILE]

Note: In the syntax above, OPTION is enclosed by a square bracket and followed by 3 dots representing that more than one option can be used at the same time and COMMAND is a specific instruction to transform a text in a text file. Moreover, FILE suggests that you can specify one file here.

The Linux “sed” Command Options

A few options are available for the sed command. I have mentioned the most used options of the command here. However, you can look into the man page for the sed command to know more about its options.

man sed

Option

Description

-i Modifies and saves the original file.
-v Displays version information, and exits.
Note: The options in Linux CLI (Command Line Interface) are case-sensitive, so be cautious when you use them.

How to Use the “sed” Command in Linux?

The sed command in Linux offers various features for text processing and manipulation. Some of the common features include:

1. Searching and Replacing

Text search and replacement is an essential part of text processing and editing. It is used to change content in many different applications such as programming, configuring files, and editing documents. From basic pattern replacement to complex conditional replacement, sed provides a variety of features to meet various text manipulation requirements.

Here are some cases of text replacing using the sed command:

A. Replace All the Occurrences of a Word

To replace all occurrences of a word in a file using sed, you can use the sed command with the g flag. Here’s the syntax:

sed 's/target/replacement/g' input_file

Here, the s in sed indicates substitution. Replace the “target” with the word you want to replace, “replacement” with the text you want to replace it with, and g to replace all occurrences within each line.

Let’s say, there is a file named file0.txt which contains the following text in it. There are multiple Bash words within each of the lines as highlighted below:

Image indicating word Bash in a text fileNow if you want to replace all the Bash words with another word, Shell for instance, you can use the sed command to accomplish this.

To replace all occurrences of a word in a file using the sed command, you can use the following syntax:

sed 's/Bash/Shell/g' file0.txt
EXPLANATION
  • s: Indicates the substitution command in sed.
  • /Bash/: Specifies the word you want to replace.
  • /Shell/: Specifies the word you want to replace with.
  • g: Stands for global, meaning to replace all occurrences on each line.
  • txt: Specify the input file where you want to perform the replacement.

Replace All the Occurrences of bash Word wth shell using sed commandUpon executing the command, all the Bash words have been replaced with Shell in each line.

Note: The sed command here does not change or modify the content of the file0.txt file permanently. To store the modified version, you can redirect it to a new file by using the following command: sed 's/bash/shell/g' file0.txt > output_file.

Further, if you want to replace all occurrences of a word in a specific line using sed, you can specify the line number followed by the substitution with the g flag. Here’s the syntax:

sed '<line_number> s/word/replacement/g' input_file

Given that the first line of file0.txt file, there are two Bash words.

To replace them all at once, you would write the following command to the terminal:

sed ‘1s/Bash/Shell/g’ file0.txt

Replace all the occurrences of Bash with Shell using sed commandAs planned, all the Bash words in the first line have been replaced with Shell by entering the above command.

B. Replace the First Occurrence of a Word in Each Line

To replace the first occurrence of a word in each line using the sed command, you can use the following syntax:

sed 's/Bash/Shell/' file0.txt

Here, I did not set occurrence globally, which means the command will replace only its default value which is the first Bash word.

Replace the First Occurrence of Word Bash in Each Line with Shell using sed commandAs expected, all the first Bash words of each line have been changed with Shell word.

C. Replace a Specific Occurrence of a Word in Each Line

To replace a specific occurrence of a word in each line using the sed command, you can utilize the /1, /2, etc flags to indicate which occurrence you want to replace. For example, to replace the second occurrence of the Bash word with Shell word in each line in the file0.txt, use:

sed 's/Bash/Shell/2' file0.txt

Here /2 has been used because the second occurrence of the Bash word of each line has to be replaced with Shell word.

Replace Specific Occurrence of Bash word in each line with Shell using sed commandAs you see only the first and last lines of the given file contain two Bash words. The second Bash word has been replaced with Shell as highlighted above.

Further, you can specify the line number for replacement. To replace the nth occurrence of a word in a specific line number, you can combine address ranges with substitution. Here’s the syntax:

sed '<line_number>s/\<word\>/replacement/<n>' input_file

For instance, There are two Bash words in the first line of our given file0.txt file.

Highlighting Word Bash in first lineIf the second Bash word needs to be changed with Shell in the first line, you can proceed with the following way:

sed '1s/Bash/Shell/2' file0.txt
EXPLANATION

1s: Indicates the substitution command is carried in the first line.

/Bash/: Specifies the word you want to replace.

/Shell/: Specifies the word you want to replace with.

2: Denotes the occurrence number of the word to replace.

file0.txt: Specify the input file where you want to perform the replacement.

Change the second occurrence of Bash with Shell in first line using sed commandAs you see, the second Bash word in the first line has been changed with the Shell word.

D. Replace the nth occurrence of Each Line

You can also select and replace if that occurs multiple times in a file by entering the pattern to match, the replacement text, and the occurrence position.

To replace a particular pattern in a line if it appears more than once, use the following command syntax:

sed 's/pattern/replacement/n' input_file

Replace /pattern/ with the pattern you want to replace, “replacement” with the text you want to replace it with, and n with the occurrence number you want to replace. input_file is the file you want to process.

Highlighting the nth occurrence of Bash in Each LineNow for our given instance, you want to replace the second occurrence of Bash word with Shell in the given file0.txt file. you would proceed with as following:

sed ‘s/Bash/Shell/2g’ file0.txt

Replace the nth occurrence of Bash in Each Line with Shell wordAs you see, the given command line changed only the second occurrence of Bash with the Shell word.

2. Removing or Deleting a Line

The option d in sed command can be used to remove lines from the file based on different criteria such as search patterns, line numbers, line number ranges, etc. Here are some take on how you can do that:

A. Delete a Specific Line of the File

To delete a specific line from a file using the sed command, the general syntax is:

sed '<line_number>d' <filename>

Here, <line_number> represents the line you want to delete, and <filename> is the name of the file. For this given instance below, line number 6 needs to be deleted.

Highlighting a specific line of a text fileYou would enter the following command to do so:

sed ‘6d' file0.txt

Deleting a specific line of a text file using sed commandAs you see, the 6th line is no longer in the display.

Note: The dollar sign ($) represent the last line of the text file. So, if you want to delete the last line, use the $d option followed by the sed command.

B. Delete Lines from Specific Range

To delete lines within a specific range in a file using sed, you can specify the starting and ending line numbers followed by the d option. Here’s the syntax:

sed '<start_line_number>,<end_line_number>d' input_file

Highlighting a specific range of lines in a text fileFor example, to delete lines 4 to 6 of the file0.txt file:

sed '4,6d' file0.txt

Deleting a specific range of lines in a text fileThis command removes lines 4 to 6 from the input file and displays it to the terminal accordingly.

C. Delete the Lines Containing Matched Patterns

To delete lines containing matched patterns from a file, you can specify the pattern to match and use the d option. Here’s the syntax:

sed '/pattern_to_match/d' input_file

Highlighting the first occurrence of word Bash of a text fileFor example, to delete lines containing the word “Bash” from the file0.txt file, you would use:

sed '/Bash/d' file0.txt

Delete the Lines Containing Bash using sed commandThus the command removes all lines containing the word “Bash” from the input file file0.txt.

3. Displaying Lines from a File

To select and display lines with the sed command, you typically use the -n option to suppress automatic printing and then specify a pattern, line number or range of lines to match the lines you want to display.

Here are some different scenarios given below on how you can select and display lines with the sed command:

A. Printing File with Line Numbers

To print line numbers alongside the lines of a file using the sed command, you would utilize the following syntax:

sed '=' example.txt | sed 'N;s/\n/ /'

This command will output each line of “example.txt” along with its corresponding line number, separated by a space.

For our given text file, you will write the following command line to the terminal:

sed '=' file0.txt | sed 'N;s/\n/ /'

Printing File with Line Numbers

B. Display Only Specific Lines of the File

To display only specific lines of a file using the sed command, you can use the syntax sed -n '<line_numbers>p' <filename>. This command prints only the lines specified by <line_numbers> from the file <filename>. For example, to display lines 3, 5, and 7 from the given file “file0.txt”, you would run:

sed -n '3p;5p;7p' file0.txt

Display Only Specific Lines of the File

Note: To display a range of lines from a file using the sed command, you can utilize the syntax sed -n '<start_line>,<end_line>p' <filename>. This command prints only the lines within the specified range, inclusive of both the start and end lines, from the file <filename>.

C. Displaying Multiple Ranges of Lines

You can use the -e (expression) option to display multiple ranges. The syntax for displaying multiple ranges of lines from a file is:

sed -n -e '<start_line_1>,<end_line_1>p' -e '<start_line_2>,<end_line_2>p' <filename>

For example, to display lines 2 to 4 and lines 6 to 8 from the file0.txt file, you would run:

sed -n -e '2,4p' -e '6,8p' file0.txt

Displaying Multiple Ranges of LinesAs you see, the image depicts only lines 2 to 4 and lines 6 to 8 as we set to the command line.

D. Display the Lines Containing Matched Patterns

To select and display lines containing a pattern, you use the following general syntax:

sed -n '/<pattern>/p' <filename>

This command prints only the lines from <filename> that match the specified <pattern>.

For instance, to display lines containing the word “Bash” from the file named “file0.txt”, you would execute:

sed -n '/Bash/p' file0.txt

Display the Lines Containing Matched PatternsHowever, to print lines starting with a particular word, you can employ a pattern match that checks if each line begins with the desired word. The general syntax is sed -n '/^<word>/p' <filename>, where <word> is the word you want to match at the beginning of the line.

For example, to print lines starting with the word “Bash” from the file0.txt file, you would execute:

sed -n '/^Bash/p' file0.txt

Display the Lines starting with word BashHere the caret sign (^) signifies that the pattern Bash should be at the beginning of the line. The symbol /p is an abbreviation for “print,” which has the same meaning as it did in the commands we used earlier.

4. Inserting and Appending Text

Similar to the functionality of replacing, deleting, or displaying a particular line, the sed command also offers the feature of inserting and appending text. You can add a line after a specific line number.

To add a line after a specific line number using the sed command, you can utilize the syntax sed '<line_number>a\<text_to_insert>' <filename>. This command inserts <text_to_insert> after the line with <line_number> in the file <filename>.

For example, to add the line “Bash scripting is also popular among the programmer” after line 5 in the file0.txt file, you would run:

sed '5a\Bash scripting is also popular among the programmer' file0.txt

Inserting line in Bash fileAs we inserted a new line in our command, the image is displaying it.

To append text to the end of each line in a file using sed, you can use the s/$ command to match the end of the line and then append the desired text. Here’s the syntax:

sed 's/$/text_to_append/' input_file

This command will add “text_to_append” to the end of each line in the input file

Conclusion

As seen in this article, the sed command has some significant uses in Linux. You’ve also found the syntax, some functional options, and some practical applications of this command. To become an expert in Linux, practice the command and its practical applications. However, if you have any questions or queries, feel free to comment below. Thank You!

People Also Ask

What is the sed command in Linux?

The sed command is a shell built-in command in Linux that allows to edit files and streams on Linux using the sed stream editor. It’s a powerful tool that can search, replace, add, or delete lines in a text file even without opening them. The sed command also supports regular expressions allowing for complex pattern matching.

How to replace text in a file using the sed command?

To replace text in a file using the sed command, use the syntax sed ‘s/old_string/new_string/’ filename.txt. This will replace the old_string with the new_string of filename.txt. For instance, to replace the word “universe” with “multiverse” in a file called “marvel.txt”, run the command sed 's/universe/multiverse/' marvel.txt in the terminal.

Does sed save the file?

No, by default, the sed command does not save changes directly to the input file. Instead, it outputs the modified content to the standard output, which you can redirect to a new file if needed.

When to use sed vs grep?

Sed excels at manipulating text to replace, insert, or delete text. Grep, on the other hand, excels at searching and filtering certain patterns or lines in files. When it comes to editing text, sed excels, and when it comes to extracting or finding specific information, grep excels. These two tools work well together and are often used in combination to perform complex text-processing operations effectively.

Is sed a text editor?

Yes. sed, short for “stream editor,” is indeed a text editor, but it functions primarily as a command-line tool for performing text processing tasks. Unlike traditional text editors such as Vim or Emacs, which provide interactive interfaces for editing files, sed operates non-interactively, processing text based on commands provided through the command line or a script.

Does sed read the whole file?

No, sed does not write the entire file to memory. Rather, it works on the file line-by-line. This makes it memory-friendly even for big files. Because of this streaming behaviour, sed can work with any file size without any memory overhead.

Does the sed command edit the source file directly?

No. The sed command does not edit the source file directly unless instructed. Rather the command displays only the changes while the original file remains the same. However, you can use the -i option with the sed command to overwrite the source file directly. Alternatively, you can save the modification in a different file by using output redirection.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Jannatul Ferdousi Sylvie

Hi there! This is Jannatul Ferdousi Sylvie. I am one of the Linux Content Developer Executives at Softeko. I live in Dhaka, Bangladesh. I have completed my BSc. in Software Engineering from American International University-Bangladesh. You can see my projects in the GitHub account. I love traveling, shopping, cooking, and gardening. Read Full Bio

Leave a Comment