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.
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
sedreads 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 {}.
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.txtRegex 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.txtMixed Addressing
# Delete from line 1 to the first line starting with "ts"
sed '1,/^ts/d' file.txtSub‑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.txtAppend ( a )
# Append a line after every line
sed 'a A' file.txt
# Append after lines 1‑2
sed '1,2a A' file.txtInsert ( i )
# Insert a line before lines 1‑2
sed '1,2i A' file.txtChange ( 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.txtDelete ( d )
# Delete lines 1‑3
sed '1,3d' file.txt
# Delete lines starting with "This"
sed '/^This/d' file.txtPrint 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.txtPractical 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.
Signed-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.
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.)
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.
