Fundamentals 12 min read

Mastering sed: Powerful Stream Editing Techniques for Linux

This tutorial introduces the Linux stream editor sed, explains its working principle, common usage scenarios, regular expression basics, essential command syntax, address selection methods, and provides practical examples to help readers efficiently perform automated text manipulation tasks.

Efficient Ops
Efficient Ops
Efficient Ops
Mastering sed: Powerful Stream Editing Techniques for Linux

Introduction

Linux users are familiar with the "three swordsmen" grep, awk, and sed; this article focuses on sed, the stream editor that processes text non‑interactively using regular expressions.

sed reads input line by line into a temporary buffer called the pattern space, applies user‑specified commands, and outputs the result, making it ideal for automation, bulk edits, and complex transformations.

How sed Works

sed reads each line, stores it in the pattern space, executes commands on that buffer, outputs the processed line, and repeats until the end of the file. The original file remains unchanged unless redirection or the

-i

option is used.

Regular Expressions

sed relies heavily on regular expressions. Basic symbols include

.

(any character),

*

(zero or more),

^

(line start),

$

(line end), character classes

[]

, and quantifiers

{m,n}

. Extended regex can be enabled with the

-r

option.

Basic Syntax

Typical command format:

sed [options] 'command' filename

. Common options include

-n

(quiet mode),

-e

(multiple commands),

-i

(in‑place edit),

-f

(script file), and

-r

(extended regex).

Addressing

Commands can target specific lines using numeric addresses (e.g.,

1,3

) or regex addresses (e.g.,

/pattern/

). Mixed addressing is also possible.

<code># Replace "hello" on line 4 with "A"
sed '4s/hello/A/g' file.txt
# Delete empty lines
sed '/^$/d' file.txt</code>

Core Subcommands

s : substitution, the most frequently used command (e.g.,

sed 's/hello/HELLO/g' file.txt

).

a : append a line after the addressed line.

i : insert a line before the addressed line.

c : replace the addressed line(s) with new text.

d : delete the addressed line(s).

= : print line numbers.

N : read the next line into the pattern space.

Practical Examples

Common one‑liners include deleting the second character of each line, swapping the first two characters, removing all digits, converting spaces to tabs, wrapping uppercase letters in parentheses, deleting every other line, and removing blank lines.

<code># Delete the second character of each line
sed -r 's/(.)(.)(.*)$/\1\3/' file.txt
# Swap first two characters
sed -r 's/(.)(.)(.*)/\2\1\3/' file.txt
# Remove all digits
sed 's/[0-9]//g' file.txt
# Replace spaces with tabs
sed -r 's/ +/\t/g' file.txt
# Wrap uppercase letters
sed -r 's/([A-Z])/(\1)/g'
# Delete every other line
sed '0~2{d}' file.txt
# Delete blank lines
sed '/^$/d' file.txt</code>

By practicing these commands and mastering regular expressions, readers can greatly improve text‑processing efficiency in everyday tasks.

Linuxcommand lineregextext processingsedstream editor
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

0 followers
Reader feedback

How this landed with the community

login 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.