Mastering sed: Essential Linux Text Processing Commands and Examples
This article provides a comprehensive guide to using the Linux sed utility, covering its purpose, syntax, options, and a wide range of practical commands for inserting, deleting, replacing, and extracting text, along with examples and visual illustrations to help readers master text processing on the command line.
About the Linux three musketeers
grep – filter keyword information in text.
sed – edit text data and modify original files.
awk – filter and extract file data, with formatted output.
What is sed
sed software itself.
Commands provided by sed.
Source data given to sed.
sed syntax format
sed [options] [sed built‑in command] [input file]
Explanation:
1. Each element (sed, options, command, input file) must be separated by at least one space.
2. The term "sed" refers to the software.
3. sed‑commands can be a single command or a combination.
4. input‑file is optional; sed can read from stdin or a pipe.sed command execution process
Conceptual flow:
sed reads a line from a file or pipe, processes the line, outputs the line, then reads the next line, and so on.sed parameters
sed modifies data in the pattern space (in‑memory). By default it does not write changes to the file; use -i to write changes back and suppress terminal output.
Example:
sed -i 's#old#new#' file.txtsed options
-n Cancel default output, often used with the p command.
-e Execute multiple sed commands in one line.
-f Followed by a script file name.
-r Use extended regular expressions.
-i Edit the file in place; without -i changes exist only in memory.
sed commands
a Append text after the specified line.
c Replace the selected line with new text.
d Delete the selected line.
D Delete part of the pattern space up to a newline (multiline mode).
i Insert text before the specified line.
h Copy pattern space to hold space.
H Append pattern space to hold space.
g Copy hold space to pattern space.
G Append hold space to pattern space.
x Exchange pattern and hold spaces.
l Print non‑printable characters.
n Clear pattern space and read next line.
N Read next line without clearing pattern space.
p Print pattern space (usually with -n).
P Print up to the first newline.
q Quit sed.
r Read data from a file.
s Substitute (replace) text.
w Write pattern space to a file.
y Translate characters.
:label Define a label.
t If the previous command succeeded, jump to the label.sed range matching
sed modify characters and cancel default output
sed insert, delete, and replace examples
Insert text after line 3: 3a Added line after line 3 Insert text before line 3: 3i Inserted line before line 3 Delete the second line: sed '2 d' file Delete lines 1 to 4: sed '1,4d' file Delete odd lines: sed '1~2d' file Delete even lines:
sed '2~2d' filesed substitution examples
# Replace the first occurrence on each line
sed 's/old/new/' file
# Global replacement on each line
sed 's/old/new/g' file
# Use # as delimiter to avoid escaping slashes
sed 's#old#new#' fileCase‑insensitive substitution (use capital I):
sed 's#my#His#Ig' filesed grouping and back‑references
# Example: extract the word after a comma
echo 'I am teacher yuchao,welcome my linux course' | sed -r 's/^.*,(.*)m.*/\1/g'
# Result: welcomesed write command (w)
# Write lines containing "computer" to game2.log
sed '/computer/w game2.log' t1.log -nsed -e option for multiple commands
sed -e '1p' -e '2p' -e '4p' t1.log -nUsing semicolon to chain commands
sed '1p;2p;4p' t1.log -nSigned-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.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.
