Fundamentals 16 min read

Mastering sed: A Comprehensive Guide to Stream Editing with Regular Expressions

This article introduces sed as a powerful stream editor for text processing, explains its pattern‑space workflow, details command syntax, addressing methods, options, meta‑characters, and provides numerous concrete examples—including deletions, substitutions, range selections, multi‑command scripts, and file I/O operations—illustrating how each feature works in practice.

ZhiKe AI
ZhiKe AI
ZhiKe AI
Mastering sed: A Comprehensive Guide to Stream Editing with Regular Expressions

Introduction

sed is a stream editor that processes input line by line, storing each line in a temporary buffer called the pattern space, applying commands, outputting the result, and then moving to the next line. The original file is unchanged unless output is redirected.

Command syntax

sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)

Addressing

Lines can be selected by numeric addresses, ranges separated by a comma (e.g., 1,3 selects lines 1 through 3), the dollar sign ($) for the last line, or regular expressions. Combinations of these selectors are allowed.

Commands

a\ : Append a line after the current line.

b label : Branch to a script label; if the label does not exist, branch to the end of the script.

c : Replace the current line with new text.

d : Delete the current line from the pattern space.

D : Delete the first line of the pattern space.

i\ : Insert text before the current line.

h : Copy the pattern space to the hold buffer.

H : Append the pattern space to the hold buffer.

g : Replace the pattern space with the hold buffer content.

G : Append the hold buffer content to the pattern space.

l : List non‑printable characters.

n : Read the next input line and start the next command cycle with it.

N : Append the next input line to the pattern space, inserting a newline between them.

p : Print the pattern space.

P : Print the first line of the pattern space.

q : Quit sed.

r file : Read lines from a file.

t label : Conditional branch; if a previous substitution succeeded, branch to the label.

T label : Branch on failure of a previous substitution.

w file : Write the pattern space to a file.

W file : Write only the first line of the pattern space to a file.

! : Apply the following command to all lines that were not selected.

s/re/string/ : Substitute the regular expression re with string .

= : Print the current line number.

# : Comment until the next newline.

g (flag for s): Global replacement.

p (flag for s): Print the changed line.

w (flag for s): Write the changed line to a file.

x : Exchange the contents of the pattern space and the hold buffer.

y : Translate characters (does not use regular expressions).

Options

-e command , --expression=command : Allow multiple editing commands.

-h , --help : Show help.

-n , --quiet , --silent : Suppress default output.

-f , --file=script-file : Specify a sed script file.

-V , --version : Show version information.

Meta‑character set

^ : Anchor to the start of a line (e.g., /^sed/).

$ : Anchor to the end of a line.

. : Match any single character except newline (e.g., /s.d/).

* : Match zero or more occurrences of the preceding element.

[] : Match any one character within the brackets (e.g., /[Ss]ed/).

[^] : Match any character not listed (e.g., /[^A-RT-Z]ed/).

(..) : Capture group (e.g., s/(love)able/\1rs/).

& : Reuse the matched string in the replacement.

\< : Anchor to the start of a word.

\> : Anchor to the end of a word.

x{m} : Repeat character x exactly m times.

x{m,} : Repeat at least m times.

x{m,n} : Repeat between m and n times.

Examples

Delete command: d

Delete the second line of example :
sed 2d example
sed '2d' example
sed "2d" example
Delete from the second line to the end of the file:
sed '2,$d' example

Quotes are required; using double quotes or no quotes will cause an error.

Delete the last line of example :
sed '$d' example
Delete all lines containing "test":
sed '/test/d' example

No space is allowed between the pattern and the d command.

Substitution command: s

Replace the first occurrence of "test" with "mytest" on each line:
sed 's/test/mytest/g' example

The delimiter / cannot be omitted.

Print only lines where a substitution occurs (using -n and p flags):
sed -n 's/^test/mytest/p' example
Append text after the matched string using &amp; :
sed 's/^test/&ok/' example

For a line "test123", the result becomes "testok123".

Replace all occurrences of "loveable" with "lovers" and print the changed lines:
sed -n 's/\(love\)able/\1rs/p' example
\1

refers to the captured "love".

Use an alternative delimiter (e.g., # ) to replace "10" with "100":
sed 's#10#100#g' example

The # character acts as the separator instead of the default /.

Selecting line ranges

Print all lines between patterns test and check :
sed -n '/test/,/check/p' example
Print lines from the fifth line up to the first line starting with "test":
sed -n '5,/^test/p' example
Append "sed test" to the end of lines between test and check :
sed '/test/,/check/s/$/sed test/' example

Multiple edits with -e

Execute two commands in order; the first substitution influences the second:
sed -e '1,5d' -e 's/test/check/' example

Each -e introduces a separate command.

Use --expression similarly to -e :
sed --expression='s/test/check/' --expression='/love/d' example

Reading from a file (r)

Read the contents of file and append them after lines matching test :
sed '/test/r file' example

Writing to a file (w)

Write lines containing test to file :
sed -n '/test/w file' example

Append command (a\)

Append "hello" after lines starting with test :
sed '/^test/a\hello' example

The a command requires a trailing backslash.

Insert command (i\)

Insert "new line" before lines containing test :
sed '/test/i
ew line' example

Next command (n)

Replace aa with bb on the line following a line that contains test :
sed '/test/{n;s/aa/bb/;}' example

Transform command (y)

Translate characters from abcde to uppercase ABCDE on lines 1 through 10:
sed '1,10y/abcde/ABCDE/' example

Regular‑expression meta‑characters cannot be used with y.

Quit command (q)

Quit after printing the tenth line:
sed '10q' example

Hold and Get (h and G)

Copy lines containing test to the hold buffer, then append that buffer to the last line:
sed -e '/test/h' -e '$G' example

Hold and Exchange (h and x)

Replace lines containing check with the contents of the hold buffer that stored a line matching test :
sed -e '/test/h' -e '/check/x' example

Scripts

A sed script is a list of commands executed with the -f option. Commands must not have trailing whitespace. Multiple commands on one line are separated by semicolons. Lines beginning with # are comments and cannot span multiple lines.

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-lineUnixregular expressionstext processingsedstream editor
ZhiKe AI
Written by

ZhiKe AI

We dissect AI-era technologies, tools, and trends with a hardcore perspective. Focused on large models, agents, MCP, function calling, and hands‑on AI development. No fluff, no hype—only actionable insights, source code, and practical ideas. Get a daily dose of intelligence to simplify tech and make efficiency tangible.

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.