Mastering sed: Powerful Text Editing and Stream Processing in Linux
This article provides a comprehensive guide to the Linux stream editor sed, covering its batch processing workflow, core options (-e, -f, -n), common commands for substitution, deletion, insertion, line addressing, pattern filtering, transliteration, and file I/O with clear examples and code snippets.
1. Stream editor sed and the sed command
In Linux, the stream editor sed is commonly used for text replacement. Unlike interactive editors such as vim, sed processes files in batch mode, making it much faster for large‑scale data editing.
Typical sed processing steps:
Read one line from input.
Match the line against the provided editing commands.
Modify the line according to the commands.
Output the new line to STDOUT.
sed repeats this cycle until all input is processed.
<code>sed [options] edit_commands [file] # items in brackets are optional</code>2. Using sed for text replacement
The
scommand performs substitution. Basic syntax:
<code>sed 's/srcStr/dstStr/' file</code>Use different delimiters if needed, e.g.
!:
<code>sed 's!/bin/bash!/BIN/BASH!' /etc/passwd</code>By default only the first occurrence per line is replaced; add the
gflag to replace all matches.
<code>sed 's/root/ROOT/g' /etc/passwd</code>The
pflag prints matching lines, often combined with
-nto suppress automatic output:
<code>sed -n 's/root/ROOT/gp' /etc/passwd</code>3. Line addressing
sed can target specific lines using numeric addresses or ranges:
<code>sed '3 s/bin/BIN/g' /etc/passwd # replace "bin" with "BIN" on line 3
sed '2,5 s/bin/BIN/g' /etc/passwd # replace on lines 2 through 5
sed '10,$ s/bin/BIN/g' /etc/passwd # replace from line 10 to the end</code>Pattern addressing filters lines that match a regular expression:
<code>sed -n '/root/s/bin/BIN/p' /etc/passwd</code>Range patterns apply commands from the first matching line to the second:
<code>sed '/pattern1/,/pattern2/ edit_command' file</code>4. Deleting lines
The
dcommand removes lines. Combine with addressing to delete specific lines:
<code>sed '/root/d' /etc/passwd # delete lines containing "root"
sed '2,$d' /etc/passwd # delete from line 2 to the end</code>5. Inserting and appending text
Use
ito insert a line before the addressed line and
ato append after it:
<code>sed 'iInsert a line before every line' /etc/passwd
sed '1iInsert a line before the first line' /etc/passwd
sed '3aAppend a line after the third line' /etc/passwd
sed '$aAppend a line at the last line' /etc/passwd</code>6. Modifying lines
The
ccommand replaces the entire addressed line(s) with new text:
<code>sed '3 cNew text' /etc/passwd # replace line 3
sed '/root/ cNew text' /etc/passwd # replace lines matching "root"
sed '2,4 cNew text' /etc/passwd # replace lines 2‑4 with a single new line</code>7. Transliteration
The
ycommand performs character‑by‑character conversion:
<code>echo abcdefggfedcba | sed 'y/acg/ACG/'</code>Result:
AbCdefGGfedCbA.
8. Working with files
Write selected lines to a file using
w:
<code>sed '1,2w test.txt' /etc/passwd</code>Read and insert a file’s content using
r:
<code>sed '3 r test.txt' /etc/passwd</code>These commands enable powerful, scriptable text manipulation directly from the command line.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.