Mastering Sed: Essential Line Selection, Options, and Commands for Linux Text Processing
This article introduces the stream editor sed, covering how to select lines, use common options, apply basic commands, and demonstrates practical scripts for cleaning and formatting text files on Linux.
sed Introduction
sed (stream editor) processes input streams, editing standard input or specified files.
Line Selection
Three types of line addresses are covered:
Single addresses: line number ( n ), last line ( $ ), or regular expression ( /regexp/ ).
Range addresses: addr1,addr2 , first~step , or addr1,+n .
Negated addresses: addr! matches all lines except the specified address.
Common Options
-n: suppress automatic printing. -e: specify multiple commands (optional for a single command). -f: read commands from a file.
Basic Commands
Commands are grouped into three categories:
Single‑character commands: q (quit), d (delete), p (print).
Commands requiring one argument: i (insert before line), a (append after line), c (replace selected lines).
Commands requiring two arguments: substitution s/regexp/replacement/[flag] and transliteration y/set1/set2/ .
Example: Simulating head
To output the first ten lines of a file:
sed '10q' file.txtThis runs until line 10, then quits, producing the same result as the head command.
Example: Cleaning a Novel Draft
The following sed script removes chapter headings, comments, and unwanted punctuation, adds indentation, and ensures blank lines around chapters:
1,/第.*章/{
/第.*章/!d
}
/^\/\//d
/\/\/.*$/s/\/\/.*$//
s/\.\.\.\.\./……/
s/\.\.\./…/
y/,./,。/
/第.*章/!s/^/ /
/第.*章/a
/第.*章/i
/\/\*/,/\*\//d
$ aKey actions performed by the script:
Delete lines outside the first chapter block.
Remove lines starting with // and inline comments.
Replace six periods with an ellipsis and three periods with a Chinese ellipsis.
Translate English commas and periods to Chinese punctuation.
Indent body text and add blank lines before and after chapter titles.
Delete multi‑line comments and append a final blank line.
Pattern Space
sed reads each line into a pattern space, applies commands, then outputs (or discards) the space before moving to the next line. The -n option suppresses the default print, while p prints the current pattern space without clearing it.
Advanced multi‑line operations and further pattern‑space tricks are available in the book “sed and awk”.
This tutorial provides a foundation for using sed effectively in Linux text processing tasks.
360 Zhihui Cloud Developer
360 Zhihui Cloud is an enterprise open service platform that aims to "aggregate data value and empower an intelligent future," leveraging 360's extensive product and technology resources to deliver platform services to customers.
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.