Mastering sed: Essential Commands and Advanced Techniques for Text Processing
This guide introduces the core syntax of the Unix sed utility, explains common options such as -n, -i, -e, and demonstrates basic commands (a, c, d, i, p, s, n, y) as well as advanced multi‑line operations using pattern and hold spaces, complete with practical code examples.
Basic sed commands
Basic syntax
sed OPTIONS… [SCRIPT] [INPUTFILE…]
Common options:
-n, --quiet: suppress automatic printing of the pattern space
-i: edit files in place (default does not modify the original file)
-e: allow multiple commands (scripts) to be applied
-f /path/from/sed_script: read the processing script from a file
-r: use extended regular expressions
sed command options
Replacement flags g: global replacement within the line w: write the line to a file x: exchange the contents of the pattern space and the hold space y: transliterate characters (not a regular‑expression operation)
Single‑line commands
a: append text after the current line (the text follows the a command)
c: replace the selected lines with new text (the text follows the c command)
d: delete the selected line(s)
i: insert text before the current line (the text follows the i command)
p: print the selected lines, usually used together with the -n option
s: substitute a pattern, e.g., 1,20s/old/new/g n: read the next input line and continue processing
y: transliterate characters, e.g.,
y/abc/ABC/ # vim xbz
cat xbz
a b c
d
c
b
a
# sed '3abbxxxx' xbz # add after line 3 (line 4)
# sed '/c/abbxxxx' xbz # add after lines matching c # cat xbz
a b c
d
c
b
a
# sed '2cxxb' xbz # replace line 2
# sed '/d/caa' xbz # replace line matching d # cat xbz
a b c
d
c
b
a
# sed '1d' xbz # delete first line
# sed '/c/d' xbz # delete lines matching c # cat xbz
a b c
d
c
b
a
# sed '2i3838' xbz # insert after line 2
# sed '/c/i6868' xbz # insert before lines matching c # cat xbz
a b c
d
c
b
a
# sed -n '/b/p' xbz # print only lines containing b # cat xbz
a b c
d
c
bbb
a
# sed 's/b/a/' xbz # replace first b on each line with a
# sed 's/b/a/g' xbz # replace all b's on each line with a # cat xbz
a b c
d
c
bbb
a
# sed -n '/a/n;p' xbz # print lines after a # cat xbz
a b c
d
c
bbb
a
# sed '3y/c/C/' xbz # transliterate c to C on line 3Advanced sed commands
Pattern space and hold space
Pattern space is the buffer that holds the current line being processed. The hold space is a secondary buffer that retains its contents across cycles and is not printed automatically. Commands such as h, H, g, G, x allow interaction between the two spaces.
Multi‑line space commands
Multi‑line space commands N: read the next line and append it to the pattern space P: print up to the first newline in the pattern space D: delete up to the first newline and restart the cycle without reading a new line
# cat xbz.txt
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.
# sed -n '/Operator$/{N;p}' xbz.txt
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
# sed -n '/Operator$/{N;s/Owner and Operator
Guide/installation Guide/g;p}' xbz.txt
Consult Section 3.1 in the installation Guide for a description of the tape drives # cat test
This is the header line.
This is a data line.
This is the last line.
# sed '/^$/{N ; /header/D}' test
This is the header line.
This is a data line.
This is the last line. # cat xxb
Here are examples of the UNIX
System. Where UNIX
System appears, it should be the UNIX
Operating System.
# sed -n '/UNIX$/p' xxb
Here are examples of the UNIX
System. Where UNIX
System appears, it should be the UNIXHold (h/H): copy or append the pattern space to the hold space
Get (g/G): copy or append the hold space back to the pattern space
Exchange (x): swap the contents of the hold and pattern spaces
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
