Sed
Sed
EDitor sed
Slides partially adapted from Kumoh National University of Technology (Korea) and NYU
Stream-Oriented Non-Interactive Editor sed
Transforming Files: sed
• Stream-oriented, non-interactive, text editor
• Look for patterns one line at a time and change lines accordingly
– like awk.
• A Unix filter
– superset of previously mentioned tools
• sed +
– regular expressions
– fast
– concise
• sed –
– hard to remember text from one line to another
– not possible to go backward in the file
– no way to do forward references like /..../+1
– no facilities to manipulate numbers
– cumbersome syntax
• awk +
– convenient numeric processing
– variables and control flow in the actions
– convenient way of accessing fields within lines
– flexible printing
– C-like syntax
• Edit any size files where editing sequence is too complicated to type in
interactive mode
• Perform “multiple global” editing functions efficiently in one pass through the
input
• Before any editing is done, all editing commands are compiled into a form to be
more efficient during the execution phase.
• All editing commands in a sed script are applied in order to each input line.
• The original input file is unchanged (sed is a filter), and the results are sent to
standard output (but can be redirected to a file).
Input
scriptfile
Input line
(Pattern Space)
Hold Space
Output
• -n - only print lines specified with the print command (or the ‘p’ flag of the
substitute (‘s’) command)
• If the first line of a scriptfile is “#n”, sed acts as though -n had been specified
• Each command consists of an address and an action, where the address can be
a regular expression or line number.
script
• When it has reached the end of the script, sed outputs the current line (pattern
space) unless the -n option has been set
• All commands in the script file are compared to, and potentially act on, all lines
in the input file
script
print cmd
output
output
input only without -n
– if the address of the command matches the line in the pattern space, the command is a
pplied 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
• If no pattern is specified, the command will be applied to all lines of the input file
• Example:
Deletion: d
[address1][,address2]d
• Delete the addressed line(s) from the pattern space; line(s) not passed to
standard output.
• A new line of input is read and editing resumes with the first command of the
script.
6d deletes line 6
[address][,address]{
command1
command2
command3
}
• Or:
• Although sed contains many editing commands, we are only going to cover the
following subset:
s - substitute
a - append
i - insert
c - change
d – delete
p - print
r - read
w - write
y - transform
= - display line number
N - append the next line to the current one
q - quit
• Syntax:
[address1[,address2]]p
• Note: if the -n or #n option has not been specified, p will cause the line to be
output twice!
• Examples:
[address(es)]s/pattern/replacement/[flags]
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
$ cat test1
first:second
one:two
$ sed 's/\(.*\):\(.*\)/\2:\1/' test1
second:first
two:one
"unix is fun“
sed 's/\([[:alpha:]]\)\([^ \n]*\)/\2\1ay/g'
--> "nixuay siay unfay"
• append
[address]a\
text
• insert
[address]i\
text
• change
[address(es)]c\
text
• Example:
• When applied to a range, the entire range is replaced by text specified with
change, not each line
– the address specifies a range of lines beginning with a line that begins with From until t
he first blank line.
• The first example replaces all lines with a single occurrence of <Mail Header
Removed>.
/^From /,/^$/c\
<Mail Headers Removed>
/^From /,/^$/{
s/^From //p
c\
<Mail Header Removed>
}
• Examples:
/black/!s/cow/horse/
substitute “horse” for “cow” on all lines
except those that contained “black”
• e.g.
$ cat tmp1
hello 1
hello 2
hello 3
$ sed '=' tmp1 | sed '{N; s/\n/<NL>/g;}'
1<NL>hello 1
2<NL>hello 2
3<NL>hello 3
$_
[address[,address]]y/abc/xyz/
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
• if you only want to transform specific characters (or a word) in the line, it is much
more difficult and requires use of the hold space
in
x – exchange the contents of the hold space and the pattern space
– 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 begi
nning of a file
• Example
• sed will, by default, send the first 100 lines of filename to standard output and
then quit processing
sed challenge