Learn by reading through in order

sed — Substitution and Extraction

Practice sed: 's/a/X/' replaces the first match per line and g replaces all, -n '2p' prints one specific line, '/two/d' deletes matching lines, and -i edits a file in place — illustrated and hands-on in a browser terminal.

Replacing Text — s/old/new/ and g

sed is a stream editor: it reads input one line at a time, transforms it with the rules you give, and writes the result out. The most common job is substitution, written as sed 's/old/new/'. s means substitute, / separates the parts, and it replaces old with new. The old part can also use the regular expressions you learned earlier (^ $ . *).

Without a trailing g, s/old/new/ replaces only the first match found on each line. Adding g at the end as in s/old/new/g replaces every matching spot on that line.

FormMeaningExample
s/old/new/Replace only the first match per linesed 's/a/X/' f
s/old/new/gReplace every match on the linesed 's/a/X/g' f
s substitution vs the g flag
sed 's/a/X/' fonly the first a per line -> Xsed 's/a/X/g' fevery a on the line -> X
Without g only the first match on a line changes; with g every match on the line is replaced.
printf 'aaa\nbbb\n' > s.txt   # create the material
sed 's/a/X/' s.txt             # only the first a -> Xaa
sed 's/a/X/g' s.txt            # every a -> XXX

① Create a two-line material file with printf 'aaa\nbbb\n' > s.txt.

② Run the basic sed substitution that turns a into X, and check that only the first character on each line is replaced.

③ Then run the form with the flag at the end that substitutes throughout the line, and check that every a becomes X. (If you run it correctly, an explanation will appear.)

Linux console
0 / 3 completed
Loading Linux Terminal...

Extracting and Deleting Lines — -n 'Np' and /pat/d

sed -n 'Np' prints only line N. You put the line number you want to show in place of N (for line 2, 2p). Since sed prints every line by default, -n turns off that automatic output, and p (print) explicitly states the line you want shown. So sed -n '2p' outputs only the second line.

sed '/pat/d' deletes the lines matching pat and outputs the rest. d is delete, and the /pat/ part is the condition that selects the lines to remove. Use it when you want to drop only the lines containing a certain word and see the rest.

FormMeaningExample
-n 'Np'Print only line Nsed -n '2p' f
/pat/dDelete lines matching patsed '/two/d' f
Extract with -n p vs delete with /pat/d
sed -n '2p' fshow only line 2 (drop the rest)sed '/two/d' fdelete the two line, keep the rest
-n 'Np' pulls out only the specified line, while /pat/d outputs everything except the matching lines.
printf 'one\ntwo\nthree\n' > lines.txt   # create the material
sed -n '2p' lines.txt                      # only line 2, two
sed '/two/d' lines.txt                     # one and three, without the two line

① Create a three-line material file with printf 'one\ntwo\nthree\n' > lines.txt.

② Combine the option that turns off automatic output with the directive that prints a specified line number, and use sed to show only the second line.

③ Then use the directive that deletes matching lines to show the rest with the line containing two removed.

Linux console
0 / 3 completed
Loading Linux Terminal...

Editing a File in Place — sed -i

So far sed has only printed results to the screen, leaving the original file unchanged. Adding sed -i edits the file directly with the result. It is handy for bulk replacements in config files, but since the original content is not kept, after rewriting let's use cat to check the contents.

FormMeaningExample
-iEdit the file in placesed -i 's/a/Z/' f
sed -i changes the file, not the screen
sed 's/a/Z/' fresult printed (f unchanged)sed -i 's/a/Z/' ff itself is rewritten
Without -i it only prints to the screen; with -i it rewrites the file itself.

sed -i cannot be undone

sed -i rewrites the file in place and does not keep the original content. Before using it on a real config file, the safe approach is to run it first without -i, check the result on screen, confirm it does what you intend, and only then add -i.

printf 'apple\nbanana\n' > fruit.txt   # create the material
sed -i 's/apple/cherry/' fruit.txt      # edit the file in place
cat fruit.txt                            # check cherry and banana

① Create a two-line material file with printf 'apple\nbanana\n' > fruit.txt.

② Using the sed substitution with the option that edits the file in place, change apple to cherry.

③ Run cat fruit.txt and check that the file itself has been rewritten to cherry and banana.

Linux console
0 / 3 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1How does sed 's/a/X/' f behave on each line?

Q2What does running sed -n '2p' f show?

Q3What does the -i in sed -i 's/a/Z/' f do?