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.
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.
Let's illustrate with a test file:
test.txt
apple
banana
apple
orange
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.
-n: turn off the default outputsed -n 's/apple/grape/p' test.txt
# result
grape
grape
Each of the three parts does one job:
-n: turns off the default line-by-line outputs/apple/grape/: performs the replacementp: prints only the lines that were successfully replacedNote that -n on its own shows nothing — it has to be paired with an output command like p.
-i: write directly back to the filesed -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
| Syntax | Purpose | Notes |
|---|---|---|
-i | Modify the file directly | Usually prints nothing |
-i.bak | Modify and create a backup | Produces a .bak file |
-n | Turn off the default output | Often paired with p |
-e | Specify a rule, repeatable | Run several rules at once |
s | Substitute text | s/old/new/ |
p | Print specified lines | Pair with -n for precise output |
d | Delete specified lines | Without -i, only affects output |
i / a | Insert before line / append after line | insert / append |
g | Replace all matches | Placed at the end of the command |
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)
sed -n '3p' file.txt # line 3
sed -n '2,5p' file.txt # lines 2–5
sed -n '$p' file.txt # last line
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.
sed -n '/error/p' log.txt # print lines containing error
sed -n '/error/Ip' log.txt # case-insensitive (GNU)
sed '2i\Hello Linux' file.txt # insert before line 2
sed '2a\Hello Linux' file.txt # append after line 2
sed -e 's/apple/grape/g' -e 's/banana/melon/g' file.txt
sed 's/apple/grape/g; s/banana/melon/g' file.txt
| Symbol | Meaning | Example |
|---|---|---|
^ | Start of line | ^DEBUG: the line starts with DEBUG |
$ | End of line | ERROR$: the line ends with ERROR |
. | Any single character | a.c matches abc |
* | Previous item repeated 0 or more times | ab*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.
sed -n '20,40p' /etc/fstab
dmesg | sed -n '/error/ip'
# 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
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.
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
-i writes straight to the file — back up before touching system config.-i first to see the result, then decide whether to write back./, switch the delimiter: s|/old/path|/new/path|g.-i backup syntax is not the same.-n only controls output; it is not a file-modifying option.-i, sed only prints — it doesn't change the file.-n turns off the default output and needs p to show anything.s///g replaces all matches on each line; without g only the first one is replaced.^ and $ prevent you from accidentally changing similar lines.-i, or use -i.bak to keep a backup.