Mastering sed: Essential Commands and Advanced Techniques for Text Processing
This article provides a comprehensive guide to the Unix stream editor sed, covering basic syntax, common options, single‑line commands, pattern‑space and hold‑space concepts, multi‑line operations, and practical code examples for both beginners and advanced users.
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 or scripts
-f /path/from/sed_script: read the 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 pattern space and hold space y: transliterate characters (not a regular‑expression operation)
Single‑line commands a: append text after the current line c: replace the selected lines with new text d: delete lines (no following text) i: insert text before the current line p: print selected lines (often used with -n) s: substitute using regular expressions, e.g., 1,20s/old/new/g n: read the next input line and continue processing y: transliterate characters, e.g., y/abc/ABC/
[root@localhost ~]# vim xbz
[root@localhost ~]# cat xbz
a b c
d
c
b
a
[root@localhost ~]# sed '3abbxxxx' xbz // add after line 3
...
[root@localhost ~]# sed '/c/abbxxxx' xbz // add after lines matching cc command usage
[root@localhost ~]# cat xbz
a b c
d
c
b
a
[root@localhost ~]# sed '2cxxb' xbz // replace line 2
...
[root@localhost ~]# sed '/d/caa' xbz // replace lines matching dd command usage
[root@localhost ~]# cat xbz
a b c
d
c
b
a
[root@localhost ~]# sed '1d' xbz // delete first line
...
[root@localhost ~]# sed '/c/d' xbz // delete lines matching ci command usage
[root@localhost ~]# cat xbz
a b c
d
c
b
a
[root@localhost ~]# sed '2i3838' xbz // insert after line 2
...
[root@localhost ~]# sed '/c/i6868' xbz // insert before lines matching cp command usage
[root@localhost ~]# cat xbz
a b c
d
c
b
a
[root@localhost ~]# sed -n '/b/p' xbz // print lines containing bs command usage
[root@localhost ~]# cat xbz
a b c
d
c
bbb
a
[root@localhost ~]# sed 's/b/a/' xbz // replace first b with a on each line
...
[root@localhost ~]# sed 's/b/a/g' xbz // replace all b with an command usage
[root@localhost ~]# cat xbz
a b c
d
c
bbb
a
[root@localhost ~]# sed -n '/a/n;p' xbz // print lines after ay command usage
[root@localhost ~]# cat xbz
a b c
d
c
bbb
a
[root@localhost ~]# 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. Hold space is a secondary buffer that retains its contents until explicitly overwritten; it is not printed automatically. Commands h, H, g, and G allow interaction between these two spaces.
Multi‑line space commands
Multiple‑line space N: append the next line 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
[root@localhost ~]# cat test
This is the header line.
This is a data line.
This is the last line.
[root@localhost ~]# sed '/^$/{N;/header/D}' test // delete the header line using multi‑line spaceHold space commands
Command
Abbreviation
Function
Hold
h (copy) or H (append)
Copy or append pattern space to hold space
Get
g or G
Copy or append hold space to pattern space
Exchange
x
Swap contents of hold and pattern spaces
[root@localhost ~]# cat abc
1
2
11
22
111
222
[root@localhost ~]# sed '/1/{h;d};/2/G' abc // store line 1 in hold space, delete it, then append hold space when line 2 matchesSigned-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.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.
