Mastering sed: A Complete Guide to Stream Editing on Linux
This article provides a thorough, step‑by‑step tutorial on the Linux stream editor sed, covering its basic workflow, essential options like -e, -f and -n, a full list of editing commands, text substitution techniques, line addressing, deletion, insertion, modification, transliteration, and file I/O operations, all illustrated with concrete command‑line examples.
1. Introduction to sed
sed is a stream editor used in Linux for batch text processing. Unlike interactive editors such as vim, sed reads input line by line, applies editing commands, and writes the result to STDOUT, making it much faster for automated modifications.
2. Basic sed workflow
For each line, sed:
Reads one line from the input.
Matches the line against the supplied editing commands.
Applies the commands to transform the line.
Outputs the transformed line.
The process repeats until all input is consumed. The generic command syntax is:
sed [options] edit_commands [file]3. Common options
-e : Add an editing command directly on the command line. Multiple -e flags allow several commands in one invocation.
-f : Read editing commands from a file, useful when many commands are needed.
-n : Suppress automatic printing; typically combined with the p command to print only matched lines.
4. Text substitution with s
The s command replaces a pattern with a replacement string: sed 's/srcStr/dstStr/' file Delimiters can be any character, e.g. sed 's!/bin/bash!/BIN/BASH!'. Flags after the command control the scope:
g : Replace all occurrences in the line.
A numeric flag (e.g., 2) replaces only the nth occurrence.
p : Print the line after substitution (often used with -n).
5. Line addressing
Commands can be limited to specific lines or ranges:
# Replace "bin" with "BIN" only on line 3
sed '3 s/bin/BIN/g' /etc/passwd
# Replace on lines 2 through 5
sed '2,5 s/bin/BIN/g' /etc/passwd
# Replace from line 10 to the end
sed '10,$ s/bin/BIN/g' /etc/passwdAddressing also works with pattern matching:
# Apply substitution only to lines containing "root"
sed '/root/ s/bin/BIN/g' /etc/passwdRange addressing with two patterns applies the command from the first match to the second:
sed -n '/root/,/nologin/ s/bin/BIN/p' /etc/passwd6. Deleting lines
# Delete lines matching "root"
sed '/root/d' /etc/passwd
# Delete from line 2 to the end
sed '2,$d' /etc/passwd7. Inserting and appending text
The i command inserts before a line, and a appends after a line. They must be used with an address:
# Insert before every line
sed 'i\Insert a line before each line' /etc/passwd
# Insert before the first line
sed '1i\Header line' /etc/passwd
# Append after the third line
sed '3a\Footer line' /etc/passwd
# Append after the last line
sed '$a\End of file' /etc/passwd8. Changing entire lines
# Replace line 3 entirely
sed '3c\New text for line 3' /etc/passwd
# Replace lines matching a pattern
sed '/root/ c\New text for root lines' /etc/passwd
# Replace a range with a single line
sed '2,4c\Combined replacement' /etc/passwd9. Transliteration with y
The y command performs character‑by‑character mapping: echo abcdefggfedcba | sed 'y/acg/ACG/' Result: AbCdefGGfedCbA.
10. File I/O commands
w writes selected lines to a file, and r reads a file and inserts its contents:
# Write first two lines to test.txt
sed '1,2w test.txt' /etc/passwd
# Insert contents of test.txt after line 3
sed '3 r test.txt' /etc/passwdSigned-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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
