Mastering sed: Powerful Text Editing Commands for Linux
This guide explains how the stream editor sed works, covering its basic workflow, command‑line options, core commands for substitution, deletion, insertion, appending, changing, transliteration, and file I/O, as well as line‑addressing techniques with practical examples.
Overview of the stream editor sed
sedis a non‑interactive stream editor used on Unix‑like systems for batch text processing. It reads input line‑by‑line, applies a sequence of editing commands, and writes the transformed stream to STDOUT. Because it works in a pipeline, sed is much faster than interactive editors such as vim for large‑scale modifications.
Basic command syntax
sed [options] 'edit_commands' [file]Square brackets denote optional parts. If file is omitted, sed reads from standard input.
Important options
-e – Append an editing command to the script. Useful when multiple commands are supplied on the command line. Example: sed -e 's/foo/bar/' -e 's/baz/qux/' file.txt -f – Read editing commands from a file. This is convenient when the script contains many commands.
# script.sed
s/root/ROOT/
s/bin/BIN/
s/home/HOME/
sed -f script.sed /etc/passwd-n – Suppress automatic printing of each pattern space. Typically combined with the p command to output only lines that match a substitution.
sed -n 's/foo/bar/p' file.txtCore sed commands
s– Substitute (text replacement). d – Delete the current line. i – Insert a line before the addressed line. a – Append a line after the addressed line. c – Change the addressed line(s) to new text. y – Transliterate characters (one‑to‑one mapping). p – Print the current line. = – Print the line number. w – Write selected lines to a file. r – Read a file and insert its contents.
Text substitution with s
Basic form: sed 's/srcStr/dstStr/' file The delimiter / can be replaced by any character to avoid escaping slashes, e.g.: sed 's!/bin/bash!/BIN/BASH!' /etc/passwd Flags that can follow the closing delimiter:
g – Replace all occurrences on the line.
n – Replace only the n ‑th occurrence on the line (e.g., sed 's/root/ROOT/2' file replaces the second root on each line).
Printing matched lines
Combine -n with the p flag to output only lines that were actually substituted:
sed -n 's/root/ROOT/p' /etc/passwdWriting results to a file
Use the w command to write only the modified lines to a separate file:
sed -n 's/root/ROOT/g w change.txt' /etc/passwdLine addressing
Addressing places a selector before a command, limiting the command to specific lines.
Numeric line: sed '3 s/bin/BIN/g' file – apply only to line 3.
Range: sed '2,5 s/bin/BIN/g' file – apply to lines 2 through 5.
From a line to the end: sed '10,$ s/bin/BIN/g' file.
Pattern address: sed -n '/root/ s/bin/BIN/p' file – apply only to lines containing root.
Pattern range: sed -n '/root/,/nologin/ s/bin/BIN/p' file.
Deleting lines
Use d with an address. Without an address, d would delete every line.
# Delete lines that contain "root"
sed '/root/d' /etc/passwd
# Delete from line 2 to the end of the file
sed '2,$d' /etc/passwdInserting and appending text
The i command inserts before the addressed line; a appends after it. These commands cannot be combined with other text on the same line.
# Insert a line before every line (for demonstration only)
sed 'i\Inserted line' file
# Insert before the first line
sed '1i\Header line' file
# Append after the third line
sed '3a\Footer line' file
# Append after the last line
sed '$a\End of file' fileChanging entire lines with c
The c command replaces the whole addressed line or range with a single new line.
# Replace line 3 entirely
sed '3c\New text for line 3' file
# Replace a range (lines 2‑4) with one line
sed '2,4c\Combined replacement' fileCharacter transliteration with y
Syntax: [address]y/inchars/outchars/. It performs a one‑to‑one mapping of characters.
echo abcdefggfedcba | sed 'y/acg/ACG/'
# Output: AbCdefGGfedCbAFile I/O commands
Write ( w ) : [address]w filename writes the addressed lines to filename.
# Write the first two lines to test.txt
sed '1,2w test.txt' /etc/passwdRead ( r ) : [address]r filename inserts the contents of filename after the addressed line.
# Insert test.txt after line 3
sed '3r test.txt' /etc/passwdCombining addressing with other commands
Addressing works with any command, not only s. For example, to replace bin with BIN only on lines that contain root: sed -n '/root/ s/bin/BIN/p' /etc/passwd Or to apply a substitution over a pattern range:
sed -n '/root/,/nologin/ s/bin/BIN/p' /etc/passwdBe aware that a pattern range starts processing at the first match of the start pattern and continues until the end pattern is found; overlapping ranges can cause a command to be applied multiple times.
Summary of typical usage patterns
Simple substitution on the whole file: sed 's/old/new/g' file Substitution with selective output: sed -n 's/old/new/gp' file Delete lines matching a pattern: sed '/pattern/d' file Insert a header line: sed '1i\Header text' file Append a footer line: sed '$a\Footer text' file Replace a block of lines with a single line: sed '5,10c\New block content' file Transliterate characters: sed 'y/abc/ABC/' file Write only changed lines to another file:
sed -n 's/pattern/repl/g w changes.txt' fileSigned-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.
