Learn sed - TOC for sed Book
A Book to Help Learn and Master sed
Definitive Guide to sed
by Daniel A. Goldman
Table of Contents
Foreword
Preface
1: Introduction to sed
sed is a 'Stream Editor'
PatSpace and HoldSpace
Introducing the s Command
Quoting Command-Line Scripts
Inputs for Book Examples
2: sed s (substitute) Command
Delimiter for s Command
sed Input from File or stdin
sed Command Line Options
-e sed Command Line Option
; sed Command Line Syntax
-f sed Command Line Option
sed Output to File or stdout
-i sed Command Line Option
3: Flags for s (substitute) Command
i (ignore case) Flag
g (global) and n (number) Flags
-n sed Command Line Option
p (print) and w (write) Flags
e (execute) and m (multi) Flags
Combining s Command Flags
4: Single Character MetaChars
Literal Character in RegEx
. (Dot) Wildcard Character)
\ (Specify Literal Character)
[ ] (Character Set)
\w \W (Word and Non-Word)
[: :] (Posix Character Class)
5: Anchor MetaChars
^ (Start of PatSpace)
$ (End of PatSpace)
\< \> \b (Word Boundaries)
\B (Not a Word Boundary)
6: Simple Repetition MetaChars
* (0 or More of Previous)
\+ (1 or More of Previous)
-r sed Command Line Option
\? (0 or 1 of Previous)
* \+ \? Compared
7: General Repetition MetaChars
\{N\} (Exact N of Previous)
\{L,\} (Low, Higher of Previous)
\{L,H\} (Low, High of Previous)
8: Other RegEx MetaChars
\| (Alternative Patterns)
\( \) (Grouping and Saving)
\` (Always Start of PatSpace)
\' (Always End of PatSpace)
9: SubEx MetaChars
& (Entire Matched Portion)
\N BackRef (Play Saved Group)
\l \u (Case for Next Character)
\L \U \E (Case for Next Span)
10: Command Addresses
Address Omitted
N Format Address
L,H Format Address
/RegEx/ Format Address
/RegEx/,/RegEx/ Address
L,/RegEx/ Format Address
/RegEx/,+N Format Address
/RegEx/,~N Format Address
First~Step Address
! (Inverts Address Match)
11: Delete PatSpace Content - dD
sed d Command (delete)
sed D Command (Delete)
12: Append, Insert, Change - aic
sed a Command (append)
sed i Command (insert)
sed c Command (change)
aic Syntax Alternatives
Cannot Edit a, i, c Results
13: Print PatSpace - pP l
sed p Command (print)
sed P Command (Print)
sed l Command (display line)
14: Read / Write File - rR wW
sed r Command (read Rfile)
sed R Command (Read Rfile)
sed w Command (write Wfile)
sed W Command (Write Wfile)
15: Read Line into PatSpace - nN
sed n Command (next line)
sed N Command (Next Line)
16: Access HoldSpace - hH gG x
sed h Command (hold)
sed H Command (Hold)
sed g Command (get)
sed G Command (Get)
sed x Command (exchange)
Examples using HoldSpace
17: Branch / Quit - : btT / qQ
sed : (Label, eg :k)
sed b Command (branch)
sed t Command (test)
sed T Command (Test)
Branching Examples
sed q Command (quit)
sed Q Command (Quit)
18: Other Actions - { } # eyvz =
sed { } (grouping)
sed # (comment marker)
sed e Command (execute)
sed y Command (transliterate)
sed v Command (version)
sed z Command (zap)
sed = Command (line #)
19: General Advice about sed
When to Use (or not Use) sed
Quoting Command-Line Scripts
Using sed within Shell Scripts
Testing sed Scripts
Structuring Input Files
20: Examples - Substitution
Do a Simple Substitution
Add Leader at Line Start
Substitute for a Span
Delete Space at Line Start
Delete Space at Line End
Substitute only for a Word
Substitute for One or More (+)
Substitute up to Something
Substitute on Certain Lines
Replace with Entire Match
Replace All or Just One
21: Examples - Line Spacing
Double Space a Stream
Triple Space a Stream
Squeeze Blank Lines to One
22: Examples - Add Some Lines
Add Line Before Lines 1, 3, ...
Add Line After Lines 2, 3, ...
Add Line Before Matching Line
Add Line After Matching Line
Add Line Before & After Line
23: Examples - Print Some Lines
Print Line #X
Print Last Line
Print Lines L to H
Print First X Lines
Print Last X Lines
Print Lines 4, 8, ...
Print Matching Line
Print Line Before Match
Print Line After Match
Number Lines in Stream
24: Examples - Delete Some Lines
Delete Line #X
Delete Last Line
Delete Lines L to H
Delete First X Lines
Delete Last X Lines
Delete Lines 1, 3, ...
Delete All Blank Lines
Delete Leading Blank Lines
Delete Up to Blank Line #1
Delete Match-Based Range
Delete Following Matched Line
Delete Serial Duplicate Lines
25: Examples - Other Short Tasks
Count Lines in Stream
Delete Duplicate Characters
Format a Phone Number
Replace First Match in File
Reverse Each Word
Reverse Each Line
Capitalize Words
Reverse Order of Lines
Add Commas to Numbers
26: Examples - Complex Tasks
Add Headers and Footers
Multi-Line Find and Replace
Set Incremental Macro Values
Delete HTML Tags
27: Related Unix Commands - grep
grep Usage
grep Command Line Options
28: Other Related Commands - 1/2
head - Print First Part of File
tail - Print Last Part of File
uniq - Handle Repeated Lines
tr - Translate Characters
29: Other Related Commands - 2/2
expr - Evaluate an Expression
seq - Print Number Sequence
30: Definitions of Special Terms
31: sed Command Line Reference
32: sed Command Reference
33: s Command Flag Reference
34: Address & Regex Reference
35: Related Books and Web Sites
Learn sed - Home Page | sed Book - Corrections