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.
sed [options] edit_commands [file] # items in brackets are optional2. Using sed for text replacement
The s command performs substitution. Basic syntax: sed 's/srcStr/dstStr/' file Use different delimiters if needed, e.g. !: sed 's!/bin/bash!/BIN/BASH!' /etc/passwd By default only the first occurrence per line is replaced; add the g flag to replace all matches. sed 's/root/ROOT/g' /etc/passwd The p flag prints matching lines, often combined with -n to suppress automatic output:
sed -n 's/root/ROOT/gp' /etc/passwd3. Line addressing
sed can target specific lines using numeric addresses or ranges:
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 endPattern addressing filters lines that match a regular expression: sed -n '/root/s/bin/BIN/p' /etc/passwd Range patterns apply commands from the first matching line to the second:
sed '/pattern1/,/pattern2/ edit_command' file4. Deleting lines
The d command removes lines. Combine with addressing to delete specific lines:
sed '/root/d' /etc/passwd # delete lines containing "root"
sed '2,$d' /etc/passwd # delete from line 2 to the end5. Inserting and appending text
Use i to insert a line before the addressed line and a to append after it:
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/passwd6. Modifying lines
The c command replaces the entire addressed line(s) with new text:
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 line7. Transliteration
The y command performs character‑by‑character conversion: echo abcdefggfedcba | sed 'y/acg/ACG/' Result: AbCdefGGfedCbA.
8. Working with files
Write selected lines to a file using w: sed '1,2w test.txt' /etc/passwd Read and insert a file’s content using r: sed '3 r test.txt' /etc/passwd These commands enable powerful, scriptable text manipulation directly from the command line.
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.
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.
