Fundamentals 14 min read

Mastering sed: Essential Commands and Real-World Examples for Text Processing

This guide introduces the stream editor sed, explains its working principle, basic syntax, regular‑expression fundamentals, address selection, and common sub‑commands such as substitute, append, insert, delete, and more, while providing practical examples and exercises to help readers efficiently manipulate text in Linux environments.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering sed: Essential Commands and Real-World Examples for Text Processing

Introduction

Linux users are familiar with the "three swords" grep, awk, and sed. This article focuses on sed (stream editor), a non‑interactive text editor that operates via commands and supports regular expressions for powerful text manipulation.

How sed Works

sed

reads input line by line, stores each line in a temporary buffer called the pattern space, applies the specified commands to that buffer, outputs the result, and then proceeds to the next line. This cycle repeats until the end of the file unless output redirection or the -i option is used to modify the file in place.

Regular Expressions Overview

sed relies heavily on regular expressions. The article briefly reviews basic regex symbols ( ., *, ^, $, {}, []) and introduces extended regex (enabled with -r) which adds ?, +, |, (), and unescaped {}.

Regex table
Regex table

Basic Syntax

The general command form is: sed [options] 'command' filename Common options include -n (quiet mode), -e (multiple commands), -i (in‑place editing), -f (script file), and -r (extended regex).

Addressing

sed can target specific lines using numeric or regex addresses.

Numeric Addressing

# Replace "hello" with "A" on line 4
sed '4s/hello/A/g' file.txt
# Replace on lines 2‑4
sed '2,4s/hello/A/g' file.txt
# From line 2, next 4 lines (2‑6)
sed '2,+4s/hello/A/g' file.txt
# Last line
sed '$s/hello/A/g' file.txt
# All but line 1
sed '1!s/hello/A/g' file.txt

Regex Addressing

# Delete lines containing "hello"
sed '/hello/d' file.txt
# Delete empty lines
sed '/^$/d' file.txt
# Delete from a line starting with "ts" to a line starting with "te"
sed '/^ts/,/^te/d' file.txt

Mixed Addressing

# Delete from line 1 to the first line starting with "ts"
sed '1,/^ts/d' file.txt

Sub‑Commands

Substitute ( s )

# Replace first occurrence of "hello" with "HELLO"
sed 's/hello/HELLO/' file.txt
# Replace all occurrences (global)
sed 's/hello/HELLO/g' file.txt
# Replace the 2nd occurrence only
sed 's/hello/A/2' file.txt
# Replace from the 2nd occurrence onward
sed 's/hello/A/2g' file.txt
# Add "#" at line start
sed 's/^/#/g' file.txt
# Append "xxx" at line end
sed 's/$/xxx/g' file.txt

Append ( a )

# Append a line after every line
sed 'a A' file.txt
# Append after lines 1‑2
sed '1,2a A' file.txt

Insert ( i )

# Insert a line before lines 1‑2
sed '1,2i A' file.txt

Change ( c )

# Replace every line with "A"
sed 'c A' file.txt
# Replace lines 1‑2 with a single "A"
sed '1,2c A' file.txt
# Replace lines 1‑2 with two lines "A" and "A"
sed '1,2c A
A' file.txt

Delete ( d )

# Delete lines 1‑3
sed '1,3d' file.txt
# Delete lines starting with "This"
sed '/^This/d' file.txt

Print Line Number ( = )

# Print line numbers for lines 1‑2
sed '1,2=' file.txt
# Print line numbers at line start
sed '=' file.txt | sed 'N;s/
/\t/'

Read Next Line ( N )

# Merge even lines into odd lines
sed 'N;s/
//' file.txt

Practical Exercises

Delete the second character of each line: sed -r 's/(.)(.)(.*)$/\1\3/' file.txt Swap the first two characters of each line: 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 in parentheses: sed -r 's/([A-Z])/(\1)/g' file.txt Delete every other line: sed '0~2{d}' file.txt Delete all blank lines: sed '/^$/d' file.txt By practicing these commands and mastering regular expressions, readers can dramatically improve text‑processing efficiency in everyday Linux tasks.

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.

regular expressionstext processingsed
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.