COMP7004 - Systems Scripting
Lecture 09: SED
Dr. Vincent Emeakaroha
vincent.emeakaroha@mtu.ie
Semester 2, 2026
Sed: Stream-oriented, Non-
Interactive Text Editor
Looks for patterns one line at a time, like grep. That is, it
operates per line contents of a file
Non-interactive text editor
Borrows syntax from the interactive editor ed which accepts
the same commands
Can be used in many ways such as:
Text substitution,
Selective printing of text files,
In-a-place editing of text files,
Non-interactive editing of text files, and many more
sed Command Types and Syntax
Syntax: sed [-n] [-e] [
command
] [file]
sed [-n] [-f scriptfile] [file]
-n - only output lines specified with the print command (or the
p flag of the substitute (s) command)
-f scriptfile - next argument is a filename containing editing
commands
-e command - the next argument is an editing command rather
than a filename, useful if multiple commands are specified
If the first line of a scriptfile is #n, sed acts as though -n had
been specified
sed Commands
sed commands have the general form
[address[, address]][!]command [arguments]
sed copies each input line into a pattern space
If the address of the command matches the line in the pattern space,
the command is applied to that line
If the command has no address, it is applied to each line as it enters
pattern space
If a command changes the line in pattern space, subsequent
commands operate on the modified line
When all commands have been read, the line in pattern space is
written to standard output and a new line is read into pattern
space
sed Address
An address can be either a line number or a pattern,
enclosed in slashes ( /pattern/ )
A pattern is described using regular expressions
Basic Regular Expression (BRE) as in grep
If no pattern is specified, the command will be applied
to all lines of the input file
To refer to the last line use: $ and the first line use: ^
sed Address
Most commands will accept two addresses
If only one address is given, the command operates only on
that line
If two comma separated addresses are given, then the
command operates on a range of lines between the first and
second address, inclusively
The ! operator can be used to negate an address, ie;
address!command causes command to be applied to all
lines that do not match address
sed Command
command is a single letter
Example: Deletion: d
[address1][,address2]d
Delete the addressed line(s) from the pattern space;
The removed line(s) are not passed to standard output.
Address and Command Examples
d deletes all lines
6d deletes line 6
/^$/d deletes all blank lines
1,10d deletes lines 1 through 10
1,/^$/d deletes from line 1 through the first blank line
/^$/,$d deletes from the first blank line through
the last line of the file
/^$/,10d deletes from the first blank line through line 10
/^ya*y/,/[0-9]$/d deletes from the first line that begins
with yay, yaay, yaaay, etc. through
the first line that ends with a digit
Examples
sed -e 'd' /etc/services
Outputs nothing
sed -e '1d' /etc/services
Delete first line and output the rest to standard output
sed -e '1,10d' /etc/services
Delete first 10 lines and output the rest
sed -e '/^#/d' /etc/services
Delete all lines starting with “#” and output the rest
sed Multiple Commands
Braces {} can be used to apply multiple commands to an address
[/pattern/[,/pattern/]]{
command1
command2
command3
}
Strange syntax:
The opening brace must be the last character on a line
The closing brace must be on a line by itself
Make sure there are no spaces following the braces
Pattern Substitution
Syntax: [address(es)]s/pattern/replacement/[flags]
pattern - search pattern
replacement - replacement string for pattern
flags - optionally any of the following
n a number from 1 to 512 indicating which
occurrence of pattern should be replaced
g global, replace all occurrences of pattern in pattern space
p print contents of pattern space
Substitute Example
s/Puff Daddy/P. Diddy/
Substitute P. Diddy for the first occurrence of Puff Daddy
in pattern space
s/Tom/Dick/2
Substitutes Dick for the second occurrence of Tom in the
pattern space
s/wood/plastic/p
Substitutes plastic for the first occurrence of wood and
outputs (prints) pattern space
Using !
If an address is followed by an exclamation point (!), the
associated command is applied to all lines that dont match
the address or address range
Examples:
1,5!d would delete all lines except 1 through 5
/black/!s/cow/horse/ would substitute horse for
cow on all lines except those that contained black
The brown cow -> The brown horse
The black cow -> The black cow
Quit
Quit causes sed to stop reading new input lines and stop
sending them to standard output
It takes at most a single line address
Once a line matching the address is reached, the script will be
terminated
This can be used to save time when you only want to process
some portion of the beginning of a file
Example: to print the first 100 lines of a file (like head) use:
sed '100q' filename
sed will, by default, send the first 100 lines of filename to
standard output and then quit processing
Manipulating Files
sed prints modified content of a file to the screen but does NOT
change the file
Two ways to change this:
Use “-i option for GNU compatible sed
Output redirection
Example
sed -e 's/coat/bag/g' fileEg01 > fileEg03
Changes all occurrences of “coat” with “bag” in file “fileEg01
The modified output is saved in a new filefileEg03
FilefileEg01” remains unchanged
sed -i -e 's/coat/bag/g' fileEg01 (Changes the content of the file)
Thank You!