Q1How does sed 's/a/X/' f behave on each line?
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.
| Form | Meaning | Example |
|---|---|---|
s/old/new/ | Replace only the first match per line | sed 's/a/X/' f |
s/old/new/g | Replace every match on the line | sed 's/a/X/g' f |
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
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.
| Form | Meaning | Example |
|---|---|---|
-n 'Np' | Print only line N | sed -n '2p' f |
/pat/d | Delete lines matching pat | sed '/two/d' f |
-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
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.
| Form | Meaning | Example |
|---|---|---|
-i | Edit the file in place | sed -i 's/a/Z/' f |
-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
Knowledge Check
Answer each question one by one.
Q2What does running sed -n '2p' f show?
Q3What does the -i in sed -i 's/a/Z/' f do?