Skip to lesson

Linux Command Line · Lesson 18 of 43

Editing streams with sed

Use sed to substitute text, delete or print selected lines and edit files in place, with the s command, addresses and safe backup habits.

Beginner to intermediate·14 min·Text, redirection and pipes·Updated 2026-11-24

UNDERSTAND IT. THEN TRY IT.

What you can do after this lesson

You will change text in streams and files without opening an editor: replace words, delete lines, extract a range, and update a configuration value in one command. sed takes a little practice, but a handful of patterns cover most real needs.

What sed is

sed stands for stream editor. It reads input one line at a time, applies the commands you give it, and writes the result to standard output. It does not change the original file unless you ask. Most of the time you use it as a filter in a pipeline or on a file name.

Substitution: s/old/new/

The s command replaces text. The slashes separate the pattern from the replacement:

bash
cd ~/project
sed 's/Linux/GNU\/Linux/' docs/notes.txt | head -n 1
echo "cat and cat" | sed 's/cat/dog/'
echo "cat and cat" | sed 's/cat/dog/g'
result
GNU/Linux is a family of operating systems.
dog and cat
dog and dog

Without a flag, only the first match on each line is replaced. The g flag (global) replaces all of them. In the first example the slash in the replacement had to be escaped with a backslash. You can choose a different separator when slashes are common, such as s|/usr/local|/opt|, which is much easier to read.

Other flags: i ignores case (GNU sed) and a number such as 2 replaces only the second match on the line. Use single quotes around the script so the shell leaves it alone.

Patterns

The pattern is a regular expression like the ones in grep, so ^ and $ anchor to the line start and end, and . matches any character. In the replacement, & stands for whatever matched, and \1 stands for the first group in parentheses when you use -E (extended expressions):

bash
echo "version 2" | sed 's/[0-9]/<&>/'
echo "Asha Sharma" | sed -E 's/(.*) (.*)/\2, \1/'
result
version <2>
Sharma, Asha

Selecting lines: addresses

An address placed before a command limits it to certain lines. It can be a line number, a range, or a pattern in slashes:

  • 3 applies to line 3, and 2,4 to lines 2 to 4.
  • $ is the last line.
  • /ERROR/ applies to lines that match the pattern.

Combined with s:

bash
sed '2s/INFO/NOTICE/' logs/app.log | head -n 3
sed '/ERROR/s/timeout/time-out/' logs/app.log | grep ERROR
result
2026-01-15 10:00:01 INFO server started
2026-01-15 10:00:05 NOTICE user asha logged in
2026-01-15 10:01:12 WARN disk usage at 81%
2026-01-15 10:02:40 ERROR database time-out
2026-01-15 10:02:44 ERROR database time-out

Deleting and printing lines

d deletes the selected lines from the output. p prints them, but because sed prints every line by default, p normally goes with -n, which turns automatic printing off:

bash
sed '/INFO/d' logs/app.log
sed -n '2,3p' docs/notes.txt
sed -n '$p' docs/notes.txt
sed '1d' data/scores.csv | head -n 2
result
2026-01-15 10:01:12 WARN disk usage at 81%
2026-01-15 10:02:40 ERROR database timeout
2026-01-15 10:02:44 ERROR database timeout
The shell reads your commands.
A terminal is the window you type in.
Practice makes them stick.
Asha,blue,82
Ben,red,67

sed '1d' is another way to drop a header line, like tail -n +2.

Several commands

Use -e for each command, or separate them with semicolons:

bash
sed -e 's/apple/APPLE/' -e 's/banana/BANANA/' data/fruit.txt | head -n 3
result
APPLE
BANANA
cherry

Editing in place with -i

The -i option writes the result back into the file. It saves a lot of time, and it can destroy a file if the command is wrong, so keep a backup by adding a suffix. GNU sed attaches it directly:

bash
cp docs/notes.txt work.txt
sed -i.bak 's/shell/SHELL/' work.txt
diff work.txt work.txt.bak
result
2c2
< The SHELL reads your commands.
---
> The shell reads your commands.

diff shows the one changed line: < marks the new version and > the backup. A good routine is to run the command without -i first, check the output, and then add -i. On macOS the -i option requires an argument, so write sed -i '' 's/a/b/' file or sed -i.bak ....

When to use something else

sed is excellent for line-oriented substitutions. For columns of data, awk is easier; for fixed edits in a text editor, nano or vim is more comfortable; for structured files such as JSON, use a proper tool like jq. Do not use regular expressions to edit HTML or JSON.

Common mistakes

Forgetting g. Only the first match on each line is replaced.

Using double quotes carelessly. The shell expands $ and backticks inside double quotes. Use single quotes for the script, and double quotes only when you must insert a variable.

Running sed -i without a backup. There is no undo.

Forgetting to escape the separator. A slash inside the pattern or replacement must be escaped, or use another separator such as |.

Exercise

Replace every ERROR with FAILURE in logs/app.log, printing the result without changing the file. Then print only lines 3 to 5, delete the lines that contain INFO, and finally change blue to navy in a copy of data/scores.csv using sed -i.bak, comparing the files with diff.

KNOWLEDGE CHECK

A quick check. You’ve got this.

Try each question. Every answer comes with an explanation.

0 / 4 understood
01 What does sed 's/cat/dog/' do to a line that contains cat twice?
02 What does sed -n '2,3p' print?
03 Which command deletes lines containing DEBUG?
04 Why use sed -i.bak when editing a file in place?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses