The sed Command in Bash

Nilesh Katuwal Mar 24, 2022
  1. Bash sed Syntax
  2. Bash sed Examples
The sed Command in Bash

The sed command is a streaming text editor that operates on the replacement concept.

It is used to discover, insert, replace, and remove file fragments. You may use this program to modify files without having to open them.

This tutorial will cover the fundamentals of the sed Linux program, its syntax, and the regular expression syntax used directly to search and replace files.

Bash sed Syntax

sed is often used to process a text stream from standard input or a file. This means you may pass the output of another command straight into the utility’s input for editing, or you can interact with a file that has already been created.

By default, all results are sent to standard output, which means they will be displayed on the screen rather than saved to a file until redirected.

The following is the command syntax:

$ sed [options] commands [file-to-edit]

file-to-edit denotes the file’s location, and options denote rules while manipulating the given file. And here are some of its primary options:

  • -n, -quiet: after each iteration, do not output the contents of the template buffer.
  • -e: commands to run for editing.
  • -f: reads editing commands from a file.
  • -i: creates a backup copy of the file before editing.
  • -l: allows you to set your line length.
  • -r: enables support for expanded regular expression syntax.
  • -s: when transferring several files, consider them as individual streams rather than one lengthy one.

Bash sed Examples

We have created a file named demosed.txt and saved it in the home directory with the following contents:

demosed

Start by displaying lines 1 through 5 from the file demosed.txt in the home directory.

We will use the p command to do this. We use the -n option to print what we need rather than the whole contents of the template buffer at each iteration.

$ sed -n '1,5p' demosed.txt

Output:

This is the first line.
This is the second line.
This is the third line.
This is the fourth line.
This is the fifth line.

To output the entire file (excluding lines 1-10):

$ sed '1,10d' demosed.txt

We use d instead of the p command to remove the text. In this case, we did not utilize the -n option to display everything because the utility displays everything that is not erased when we use the delete command, but instead used the d command to clear the unwanted.

Output:

This is the eleventh line.
This is the twelveth line.
This is the thirteenth line.
This is the fifteenth line.

As expected, only lines from 11 to 15 are printed.

When we open demosed.txt, we will see the same command as when the previous command was run.

For security concerns, sed does not alter the source file by default. The -i option, which signifies edit in place, can be used to tamper with the original file.

To delete the 14th line from the file:

$ sed -i '13~15d' demosed.txt

The above command doesn’t give any output. Let’s see the content inside the file.

demosed1

Compared with the older version of the file, we see that line 14th with the text This is thirteenth line. is missing.

Regex can also be used in sed with options. Let’s look at an example.

$ sed '/^nine\|^$\| *nine/d' demosed.txt

Output:

This is the first line.
This is the second line.
This is the third line.
This is the fourth line.
This is the fifth line.
This is the sixth line.
This is the seventh line.
This is the eighth line.
This is the tenth line.
This is the eleventh line.
This is the twelveth line.
This is the fifteenth line.

'/^nine\|^$\| *nine/' is the regex operation that detects the sentence with the word nine. The above command won’t tamper with the original file because we haven’t used -i, but we can see that line with nine is filtered out.

Related Article - Bash Command