← Back to Home

Linux sed Quick Reference: Making Stream Editing Simple

2026.10.12 · Linux · 6 min read

The trouble with sed usually isn't that you don't know how to use it — it's that when the three options -i, -n, and p get mixed together, you can't tell whether the file actually changed or what the output will show. This post makes the relationship between those three clear, and everything after that gets much easier.

What sed Does

sed (Stream Editor) reads text line by line and searches, replaces, deletes, inserts, or prints according to rules.

sed [options] 'rules' filename
The single most important sentence: without -i, sed only prints the result to the terminal — it never touches the original file.

Core Idea: Printing and Writing Are Two Different Things

Let's illustrate with a test file:

test.txt
apple
banana
apple
orange

A. Default: print only, no file changes

sed 's/apple/grape/' test.txt

# result
grape
banana
grape
orange

Every line gets printed; s/apple/grape/ replaces only the first match on each line. The original file stays completely unchanged.

B. -n: turn off the default output

sed -n 's/apple/grape/p' test.txt

# result
grape
grape

Each of the three parts does one job:

Note that -n on its own shows nothing — it has to be paired with an output command like p.

C. -i: write directly back to the file

sed -i 's/apple/grape/g' test.txt

The changes are written back to test.txt, and the terminal usually prints nothing. g means every match on each line gets replaced.

If you want to keep a backup:

sed -i.bak 's/apple/grape/g' test.txt
# creates test.txt.bak

Common Options and Commands

SyntaxPurposeNotes
-iModify the file directlyUsually prints nothing
-i.bakModify and create a backupProduces a .bak file
-nTurn off the default outputOften paired with p
-eSpecify a rule, repeatableRun several rules at once
sSubstitute texts/old/new/
pPrint specified linesPair with -n for precise output
dDelete specified linesWithout -i, only affects output
i / aInsert before line / append after lineinsert / append
gReplace all matchesPlaced at the end of the command

Common Examples

Replace text

sed 's/old/new/' file.txt        # first match only
sed 's/old/new/g' file.txt       # replace all
sed -i 's/old/new/g' file.txt    # write back to the file
sed 's/old/new/gi' file.txt      # case-insensitive (GNU)

Print specified lines

sed -n '3p' file.txt       # line 3
sed -n '2,5p' file.txt     # lines 2–5
sed -n '$p' file.txt       # last line

Delete specified lines

sed '3d' file.txt          # drop line 3 from the output
sed '2,5d' file.txt        # delete lines 2–5
sed '/^$/d' file.txt       # delete blank lines
sed -i '3d' file.txt       # write back to the file

Without -i, d just keeps that line out of the output — the file content is still there.

Find matching lines

sed -n '/error/p' log.txt      # print lines containing error
sed -n '/error/Ip' log.txt     # case-insensitive (GNU)

Insert and append

sed '2i\Hello Linux' file.txt   # insert before line 2
sed '2a\Hello Linux' file.txt   # append after line 2

Run multiple rules at once

sed -e 's/apple/grape/g' -e 's/banana/melon/g' file.txt
sed 's/apple/grape/g; s/banana/melon/g' file.txt

Regular Expressions: The Key to Avoiding Bad Edits

SymbolMeaningExample
^Start of line^DEBUG: the line starts with DEBUG
$End of lineERROR$: the line ends with ERROR
.Any single charactera.c matches abc
*Previous item repeated 0 or more timesab*c
[]Character set[0-9] one digit

Practical use: only change the setting where the whole line is exactly DEBUG=0.

sed 's/^DEBUG=0$/DEBUG=1/' config.txt

Adding ^ and $ prevents you from accidentally changing lines like MY_DEBUG=0 or DEBUG=01.

Common Uses in Linux Driver / Kernel Work

View a section of a config file

sed -n '20,40p' /etc/fstab

Filter errors out of the kernel log

dmesg | sed -n '/error/ip'

Change a setting (verify first, then write back)

# First check the result without -i
sed 's/^DEBUG=0$/DEBUG=1/' config.txt

# Once it looks right, write back and keep a backup
sed -i.bak 's/^DEBUG=0$/DEBUG=1/' config.txt

Batch-edit multiple config files

sed -i.bak 's/DEBUG=0/DEBUG=1/g' ./*.conf
This modifies every *.conf in the current directory and creates backups. Check the file list and the replacement rule before running it.

The Four Lines Most Worth Knowing First

sed -n '1,20p' file.txt          # show lines 1–20
sed -n '/error/ip' file.txt      # print only lines containing error
sed 's/old/new/g' file.txt       # replace all (original file unchanged)
sed -i.bak 's/old/new/g' file.txt # modify and keep a backup

Safety Notes

Key Takeaways