Using the sed stream editor

Prefer a video?

Watch the YouTube video of this session

Learning outcomes

  • Learners can use sed to filter for lines using a regular expression
  • Learners can use sed to replace lines using a regular expression
  • Learners can use sed with different types of input and output
  • Learners can use sed with the two different regular expression types
  • (optional) Learners have seen three sed learning resources
For teachers

Lesson plan:

Time Minutes Duration Description
11:20-10:30 0-10 10 Prior
11:30-10:35 10-15 5 Present
11:35-10:55 15-35 20 Challenge
11:55-12:09 35-45 10 Feedback and conclusion

Prior:

  • How to replace text with grep?
  • What is sed?
  • sed is a stream editor. What would that mean?
  • sed is also called ‘a non-interactive command-line text editor’. What would that mean?
  • sed is a stream editor that can filter text. What would that mean?
  • sed is a stream editor that can transform text. What would that mean?

1. Why use sed?

sed finds text that matches a regular expression and can replace it:

cat macbeth.txt | sed 's/[A-Z][a-z][a-z]*land/Sweden/g'

sed is among the list of ‘Portable Operating System Interface’ (POSIX) commands, which means it is considered a fundamental tool and is likely to be available on your operating system.

sed (short for ‘stream editor’) can do what grep can do and more: it is ‘a non-interactive command-line text editor’ (source)

In this session, we will be manipulating a file from sed (instead of doing so by hand).

2. Overview

Mindmap

3. Types of operations

3.1. Filtering

sed can do what grep can do. For example, in the session aboutgrep, we used the following command:

man grep | grep "^[A-Z]"

The equivalent sed command is this:

man grep | sed --quiet "/^[A-Z]/p"
Are there synonyms for this sed command?

Yes, these commands are all equivalent:

man grep | sed --quiet "/^[A-Z]/p"
man grep | sed --silent "/^[A-Z]/p"
man grep | sed -n "/^[A-Z]/p"

In this session, --quiet is used, as it is felt to be the most self-explanatory: to me, ‘quiet’ feels that it may not be perfectly ‘silent’.

3.2. Replacing

Probably the most used feature of sed is its replacement functionality:

sed 's/[regular_expression]/[replacement]/'

The s is short for ‘substitute’. For example, the command below substitutes ‘morning’ for ‘afternoon’.

echo "Good morning" | sed 's/morning/afternoon/'
What is the output of that command?
Good afternoon

If there may be multiple matches in a sentence, add g at the end:

echo "Good morning, good morning" | sed 's/morning/afternoon/g'
What is the output of that command (with the g)?
Good afternoon, good afternoon
What is the output of that command without the g?
Good afternoon, good morning

4. Input and output

sed command Input and output
cat my_input_file.txt | sed '...' Get input from a pipe, write output to terminal
sed '...' my_input_file.txt Get input from a file, write output to terminal
sed --in-place '...' my_file.txt :warning: Modify the file directly

5. Type of regular expressions

There are two types of regular expressions present in sed according the sed manual: basic and extended regular expressions.

Here is a quote from the sed manual regarding their differences:

In GNU sed, the only difference between basic and extended regular expressions is in the behavior of a few special characters: ?, +, parentheses, braces ({}), and |.

The difference can be shown using this text:

Bland
England
Gland
Holland

To select the countries, use:

cat lands.txt | sed --quiet '/[A-Z][a-z][a-z]*land/p'
cat lands.txt | sed --quiet --regexp-extended '/[A-Z][a-z]+land/p'
This line gives me the error unrecognized option '--quiet'

Some operating systems do not support the longer options. In that case, use the syntax below instead:

cat lands.txt | sed -n --regexp-extended '/[A-Z][a-z]+land/p'

This teaching material used the longer writing, as this is easier to read.

This line gives me the error unrecognized option '--regexp-extended'

Some operating systems do not support the longer options. In that case, use the syntax below instead:

cat lands.txt | sed --quiet -E '/[A-Z][a-z]+land/p'

This teaching material used the longer writing, as this is easier to read.

Exercises

Exercise 1

Macbeth

Macbeth

source

In this exercise, we will work Macbeth, a tale written by William Shakespeare.

In these exercises, we will:

  • Replace ‘Weird Sisters’ by ‘witches’: the text will be more clear
  • Replace all country names by ‘Sweden’ (or your favorite country): the text may be funnier to read :-)

Exercise 1.1: download Macbeth

Download the file from a terminal as such:

wget https://raw.githubusercontent.com/UPPMAX/linux-command-line-201/refs/heads/main/docs/sessions/sed/macbeth.txt

Exercise 1.2: use sed to replace text from standard input

Read the ‘Replacing’ section.

In Macbeth, replace Weird Sisters (both words start with an upper-case character) by witches. Do so by using cat on the file macbeth.txt, then piping it to sed.

Answer

Here is how to show the text of Macbeth, with the text replaced:

cat macbeth.txt | sed 's/Weird Sisters/witches/'

There is no need to end with a g, as doing so (see command below) gives identical results:

cat macbeth.txt | sed 's/Weird Sisters/witches/g'

You can check in many ways that Weird Sisters only occurs once per line. For example, the command below gives no matches:

cat macbeth.txt | grep "Weird Sisters.*Weird Sisters"

Check that your replacement worked.

Tip

Pipe the output to grep to detect matches with witches

Answer
cat macbeth.txt | sed 's/Weird Sisters/witches/g' | grep witches

This gives the output:

The witches, hand in hand,
title, before, these witches saluted me, and referred me to the
I dreamt last night of the three witches:
(And betimes I will) to the witches:
Saw you the witches?
How to save to a file?

You can redirect the output to a file using >, e.g.:

cat macbeth.txt | sed 's/Weird Sisters/witches/g' > macbeth_with_witches.txt

Exercise 1.3: use sed to find text from standard input

Read the ‘Filtering’ section.

In Macbeth, there are many place names ending on land.

Search for all place names ending on land using sed. To be precise, search for all matches that:

  • (1) start with an uppercase character
  • (2) have zero or more lowercase characters
  • (3) end on land

Do so by using cat on the file macbeth.txt, then piping it to sed.

Answer
cat macbeth.txt | sed --quiet '/[A-Z][a-z]*land/p'

Exercise 1.4: use extended regular expressions

Reading your answer in the previous exercise, your non-Swedish non-Finnish colleague comes to you and states that your regular expression makes no sense: your regular expression matches ‘Aland’, ‘Bland’, ‘Cland’ (and ‘Gland’), which can be improved upon.

Why is the colleague non-Swedish non-Finnish?

Because he/she does not know that Sweden has an island called Öland and Finland has an island called Åland.

Read the section ‘Type of regular expressions’.

Search for all place names ending on land using sed. To be precise, search for all matches that:

  • (1) start with an uppercase character
  • (2) have one or more lowercase characters: use a + in your regular expression
  • (3) end on land.
Answer
cat macbeth.txt | sed --quiet --regexp-extended '/[A-Z][a-z]+land/p'

This is equivalent to the syntax below.

cat macbeth.txt | sed --quiet '/[A-Z][a-z][a-z]*land/p'

Exercise 1.5: use sed to replace text in a file

Until now, we never have touched the original file. Here we use sed --in-place [commands] [filename] to directly work on the original file.

Using sed directly on the file macbeth.txt, replace Weird Sisters by witches

Answer
sed --in-place 's/Weird Sisters/witches/g' macbeth.txt

Using sed directly on the file macbeth.txt, replace ‘lands’ by Sweden.

Answer

These are all valid answers:

sed --in-place 's/[A-Z][a-z][a-z]*land/Sweden/' macbeth.txt
sed --in-place 's/[A-Z][a-z][a-z]*land/Sweden/g' macbeth.txt
sed --in-place --regexp-extended 's/[A-Z][a-z]+land/Sweden/' macbeth.txt
sed --in-place --regexp-extended 's/[A-Z][a-z]+land/Sweden/g' macbeth.txt

Using sed directly on the file macbeth.txt, to remove the copyright.

Answer
sed --in-place '1,26d;4173,4495d' macbeth.txt

(optional) Exercise 1.6: Do this exercise from a script

Do this exercise from a script.

Answer

This is simply putting the answers in one file:

wget https://raw.githubusercontent.com/UPPMAX/linux-command-line-201/refs/heads/main/docs/sessions/sed/macbeth.txt
sed --in-place 's/Weird Sisters/witches/g' macbeth.txt
sed --in-place 's/[A-Z][a-z][a-z]*land/Sweden/' macbeth.txt
sed --in-place '1,26d;4173,4495d' macbeth.txt

Optional is to add a shebang as the first line:

#!/bin/bash

Optional is to remove macbeth.txt if it already exists before downloading, as shown below. The --force makes sure that this commands ‘works’ even if the file is absent.

rm --force macbeth.txt

You can obtain this script by:

wget https://raw.githubusercontent.com/UPPMAX/linux-command-line-201/refs/heads/main/docs/lesson_plans/20251204/script_1.sh

(optional) Exercise 1.7: Do this exercise from a smart script

Do this exercise from a script, without hardcoding the lines

Answer

Here we extract the line numbers:

#!/bin/bash
rm -f macbeth.txt
wget https://raw.githubusercontent.com/UPPMAX/linux-command-line-201/refs/heads/main/docs/sessions/sed/macbeth.txt
sed --in-place 's/Weird Sisters/witches/g' macbeth.txt
sed --in-place 's/[A-Z][a-z][a-z]*land/Sweden/' macbeth.txt

end_of_first_copyright_line=$(cat --number macbeth.txt | grep "START OF THE PROJECT GUTENBERG EBOOK" | cut --field 1)
begin_of_second_copyright_line=$(cat --number macbeth.txt | grep "START: FULL LICENSE" | cut --field 1)
last_line=$(wc --lines macbeth.txt | cut --field 1 --delimiter " ")

sed --in-place "1,${end_of_first_copyright_line}d;${begin_of_second_copyright_line},${last_line}d" macbeth.txt

You can obtain this script by:

wget https://raw.githubusercontent.com/UPPMAX/linux-command-line-201/refs/heads/main/docs/lesson_plans/20251204/script_2.sh

Summary

  • sed can do what grep can do and more
  • sed is a ‘stream editor’ and also called ‘a non-interactive command-line text editor’
  • sed can use multiple combinations of input and output
  • sed can do multiple operations, such as filtering and substituting
  • sed can use two regular expression syntaxes
  • sed is not a programming language: use AWK instead
Overview of some POSIX tools

Overview of some POSIX tools

Overview of some POSIX tools