Linux sed Command: How To Use the Stream Editor

April 6, 2022

Introduction

SED is a text stream editor used on Unix systems to edit files quickly and efficiently. The tool searches through, replaces, adds, and deletes lines in a text file without opening the file in a text editor.

Learn how to use the sed command and its options through easy-to-follow examples.

How to use the Linux sed command.

Linux sed Syntax

The main syntax for using the Linux sed command is:

sed OPTIONS... [SCRIPT] [INPUTFILE...]

sed Linux Options

You can execute sed with the following command-line options:

-b, --binaryOpen input files in binary mode to consider lines to end at a line feed.
--debugSwitch to debug mode to print input in canonical form and annotate program execution.
--follow-symlinksEdit the ultimate destination if the specified file is a symbolic link. It only works when combined with the -i option.
--helpDisplay the usage information.
--i, --in-place [=SUFFIX]Perform edits in-place by overwriting the original file.
--posixDisable all extensions to POSIX sed to simplify writing portable scripts.
--versionDisplay the version of sed running on the system.
-E, -r, --regexp-extendedUse extended regular expressions.
-e script, --expression=scriptAdd a specified script to run with the commands.
-f script-fileAdd the contents of a specified script-file to run with the commands.
-l N, --line-length=NDefine the desired line-wrap length for the l command (default value is 70).
-n, --quiet, --silentDisable output printing.
-s, --separateView specified files as separate, not as a single continuous long stream.
--sandboxDisable running external programs and operate only on input files on the command line.
-u, --unbufferedMinimalize input and output buffer.
-z, --null-data, --zero-terminatedView input as a set of lines where each ends with a zero byte.

Linux sed Examples

Below we show ten commonly used sed commands, examples, and syntax.

For this tutorial, we created a sample file under the name foxinbox.txt with the following content:

Knox in box.
Fox in socks.

Knox on fox in socks in box.

Socks on Knox and Knox in box.
Fox in socks on box on Knox.

Note that sed does not affect the source file unless instructed. Instead, the command displays the changes made by the given command in its output.

To overwrite the original file, use the -i option to save the modifications. However, such practice is not recommended before testing out the command output.

Alternatively, save the edits to a different (or new) file. Redirect the output by adding > newfilename.txt at the end of the command.

Replace String Using the sed Command

The Linux sed command is most commonly used for substituting text. It searches for the specified pattern in a file and replaces it with the wanted string.

To replace text using sed, use the substitute command s and delimiters (in most cases, slashes - /) for separating text fields.

Therefore, the syntax for replacing text is:

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

Replace old_string with the text you want to substitute and new_string with the text you want to change it to.

For example, to replace instances of box with the word bin, run:

sed 's/box/bin/' foxinbox.txt

The output shows the text with the replaced words, as in the image below.

Replace text in file using the sed command.

Note: Learn more about using sed to replace strings with our guide How to Use Sed to Find and Replace a String in a File.

Replace All Occurrences of String Using the sed Command

By default, sed only replaces the first occurrence of the specified string in each line. It searches for the first instance of the specified word in a line, replaces it, and moves on to the next line.

With multiple instances of the word box in the same line, the command only substitutes the first.

sed command replacing only the first occurrence of a word in a line.

If you have multiple instances of the same word within a single line, add the g flag to the command to change all of them.

The command for substituting each occurrence of a given string within a text is:

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

To replace the word box with the word bin in the file foxinbox.txt every time, type:

sed 's/box/bin/g' foxinbox.txt
Replace all instances of a word in a file using the sed command.

The output above shows that the command replaced all instances of the word box.

Replace Specific Occurrence in a Line Using the sed Command

The sed command lets you choose which occurrence of a specified string you want to replace within each line. To do so, add a number flag such as 1, 2, etc.:

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

For example, to substitute the second occurrence of the word box in each line of the text with the word bin, use this command:

sed 's/box/bin/2' foxinbox.txt
Replace the nth occurrence of a word in a line using the sed command.

The command skipped three instances of the word because they were first in line and replaced only the one at the end.

Only Print Lines With Substitute Text

By default, the sed command prints out the entire file content, along with the substitute text in its output. If you have a lot of text and want to focus on the lines with the applied changes, add the needed attributes to the command.

To print out just the lines that have substitution under the given conditions, use the syntax:

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

The -n option disables automatic printing, while the substitute command p instructs sed to print lines where substitution occurs.

You can add the p command to other substitute commands, as in the example below:

sed -n 's/box/bin/2p' foxinbox.txt

The command replaces the second instance of the word box in a line and prints the line where the change took place. In the image below, you see only one line with the applied substitution.

the output of sed command displaying only one line with the applied substitution

Replace String Using the sed Command and Ignore Case

By default, the sed command is case-sensitive. For instance, the following content contains the word fox with upper and lowercase first letters:

Knox in box.
Fox in socks.

Knox on fox in socks in box.

Socks on Knox and Knox in box.
Fox in socks on box on Knox.

To substitute all the instances of the word fox, do not run the usual command:

sed 's/fox/cow/' foxinbox.txt

The result of the command only changes the occurrence of the word fox where it is entirely lowercase:

sed command substituting text in its default case-sensitive mode.

To ignore case while substituting text, add the i subcommand at the end of the command:

sed 's/old_string/new_string/i' filename.txt

Hence, the command for changing upper and lowercase instances of the word fox in the text above is:

sed 's/fox/cow/i' foxinbox.txt

The output shows the command changed all three instances of the specified string.

The output of sed command changing all three instances of the specified string

Replace String in Specific Line Using the sed Command

The sed command allows you to substitute a string in a specific line by adding the line number as a prefix to the s subcommand:

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

To test this feature on the sample foxinbox.txt file, replace the word socks with sandals only in the fourth line (4) of the text using the command:

sed '4 s/socks/sandals/' foxinbox.txt

The output shows that the first instance of the word socks in the second line remains the same, while the same word has been replaced with sandals in the fourth line.

Replace string in specified line using the sed command.

Replace String Only in Specific Range of Lines Using the sed Command

To replace multiple instances of a string within a line range, but not the entire text, specify the range where you want sed to substitute. The syntax is:

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

Replace the first # with the initial line number and the second # with the last line number you want to include.

For instance, to replace the last two instances of the word socks in the file foxinbox.txt (located in the fourth and sixth line) with the word sandals, you would run:

sed '4,6 s/socks/sandals/' foxinbox.txt
Replace String Only in Specified Range of Lines Using the sed Command.

Delete Specific Line Using the sed Command

To delete a line from a file with the sed command, use the d subcommand and the syntax:

sed '#d' filename.txt

Specify the line number you want to remove instead of the hash (#) symbol and run the command.

For instance, to remove the second line from the foxinbox.txt file, type:

sed '2d' foxinbox.txt
Delete specified line using the sed command.

Delete Lines Within a Specific Range of Lines Using the sed Command

To delete multiple lines within a line range using the sed command, follow the syntax:

sed '#,#d' filename.txt

Replace the hash symbols with the beginning and end of the line range. For example:

sed '2,4d' foxinbox.txt

The command above deletes lines 2, 3, and 4 from the file.

Delete line range from the file using the sed command.

Delete From Specific to Last Line Using the sed Command

To delete lines from a specific line number to the last line, use the command:

sed '#,$d' filename.txt

That is, to remove everything from line 3 to the end of foxinbox.txt, run this command:

sed '3,$d' foxinbox.txt
Delete lines from specific line number to end with sed.

Conclusion

In this article, you learned how to use the text stream editor SED to modify a file through the command line. Once you have made the changes to the text and saved the latest version, decide whether or not to keep the original file.

If working with sensitive data, you may want to permanently delete the file using the shred command.

For a list of all the important Linux commands in one place, check out Linux Command Cheat Sheet.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Use the less Command in Linux with Examples
March 23, 2022

The less command is a Linux terminal pager allowing you to open large files in the terminal...
Read more
dpkg Command in Linux With Examples
March 29, 2022

The dpkg command in Linux helps unpack Debian packages (.deb) and install software on Debian...
Read more
The pwd Linux Command
December 22, 2021

The pwd command outputs your current working directory in Linux. This tutorial shows how to use...
Read more
How to Use the Linux at Command
February 2, 2022

The at command allows you to schedule a job for one-time execution. This guide shows...
Read more