Operations 11 min read

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.

Raymond Ops
Raymond Ops
Raymond Ops
Mastering sed: Essential Commands and Advanced Techniques for Text Processing

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 c

c 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 d

d 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 c

i 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 c

p command usage

[root@localhost ~]# cat xbz
a b c
d
c
b
a
[root@localhost ~]# sed -n '/b/p' xbz   // print lines containing b

s 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 a

n command usage

[root@localhost ~]# cat xbz
a b c
d
c
bbb
a
[root@localhost ~]# sed -n '/a/n;p' xbz   // print lines after a

y 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 3

Advanced 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 space

Hold 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 matches
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

command-lineUnixtext processingsedstream editor
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.