Sed - An Introduction and Tutorial by Bruce Barnett
Quick Links - NEW
Sed Commands
: label
# comment
{....} Block
= - print line number
a \ - Append
b label - Branch
c \ - change
d and D - Delete
g and G - Get
h and H - Hold
i \ - Insert
l - Look
n and N - Next
p and P - Print
q - Quit
r filename - Read File
s/..../..../ - Substitute
t label - Test
w filename - Write Filename
x - eXchange
y/..../..../ - Transform
Sed Pattern Flags
/g - Global
/I - Ignore Case
/p - Print
/w filename - Write Filename
Sed Command Line options
-e script (--expression=script)
-f scriptfile (--file=scriptfile)
-h (--help)
-n (--quiet --silent)
-V (--version)
Table of Contents
The Awful Truth about sed
The essential command: s for substitution
The slash as a delimiter
Using & as the matched string
Using \1 to keep part of the pattern
Sed Pattern Flags
/g - Global replacement
Is sed recursive?
/1, /2, etc. Specifying which occurrence
/p - print
Write to a file with /w filename
/I - Ignore Case
Combining substitution flags
Arguments and invocation of sed
Multiple commands with -e command
Filenames on the command line
sed -n: no printing
Using 'sed /pattern/'
Using 'sed -n /pattern/p' to duplicate the function of grep
sed -f scriptname
sed in shell scripts
Quoting multiple sed lines in the C shell
Quoting multiple sed lines in the Bourne shell
sed -v
sed -h
A sed interpreter script
Sed Comments
Passing arguments into a sed script
Using sed in a shell here-is document
Multiple commands and order of execution
Addresses and Ranges of Text
Restricting to a line number
Patterns
Ranges by line number
Ranges by patterns
Delete with d
Printing with p
Reversing the restriction with !
Relationships between d, p, and !
The q or quit command
Grouping with { and }
Operating in a pattern range except for the patterns
Writing a file with the 'w' command
Reading in a file with the 'r' command
SunOS and the # Comment Command
Adding, Changing, Inserting new lines
Append a line with 'a'
Insert a line with 'i'
Change a line with 'c'
Leading tabs and spaces in a sed script
Adding more than one line
Adding lines and the pattern space
Address ranges and the above commands
Multi-Line Patterns
Print line number with =
Transform with y
Displaying control characters with a l
Working with Multiple Lines
Using newlines in sed scripts
The Hold Buffer
Exchange with x
Example of Context Grep
Hold with h or H
Keeping more than one line in the hold buffer
Get with g or G
Flow Control
Testing with t
Debugging with l
An alternate way of adding comments
The poorly documented ;
Passing regular expressions as arguments
Command Summary
In Conclusion
More References
Copyright 1994, 1995 Bruce Barnett and General Electric Company
Copyright 2001,2005,2007,2011,2013 Bruce Barnett
All rights reserved
You are allowed to print copies of this tutorial for your personal use, and link to this page, but you are not allowed to make electronic copies, or redistribute this tutorial in any form without permission.
Original version written in 1994 and published in the Sun Observer
Introduction to Sed
How to use sed, a special editor for modifying files automatically. If you want to write a program to make changes in a file, sed is the tool to use.
There are a few programs that are the real workhorse in the UNIX toolbox. These programs are simple to use for simple applications, yet have a rich set of commands for performing complex actions. Don't let the complex potential of a program keep you from making use of the simpler aspects. I'll start with the simple concepts and introduce the advanced topics later on. When I first wrote this, most versions of sed did not allow you to place comments inside the script. Lines starting with the '#' characters are comments. Newer versions of sed may support comments at the end of the line as well.