Introducing UNIX and Linux |
Overview |
Stream editorWhereas Grep selects lines from input and copies those lines to
standard output, Sed will in addition change those lines if
required. Just as with Grep, Sed takes a script
either as an argument, or from a file by using option
address command arguments where address and/or arguments are optional. The address indicates which lines of the input command is to be performed on. Actually, we need to be slightly more precise than this. Each time a line of input is read, it is first of all stored in an area called the pattern space. The instructions forming the script are then examined one-by-one, in order, and each instruction whose address matches the address of the input line has its command applied to whatever is currently in the pattern space. When all the instructions in the script have been examined, the contents of the pattern space are copied to the standard output, and the pattern space emptied ready for the next input line. This is repeated for the next line of input until the input is exhausted. The simplest Sed script is the script containing nothing; since
there are no instructions, an input line is copied to the pattern
space which is then immediately copied to the standard output. Try
it: Both
deletes lines 1 to 4 inclusive from the input. Try the following using the standard input:
An often used command is
The pattern space is searched from left to right to find an
instance of the BRE. If none is found, no change is made, otherwise
the string that matches the BRE is replaced by the
replacement. Normally only the first occurrence of the BRE
is altered, but if you follow the command with
changes all occurrences of
changes each equals symbol at the start of a line to a question-mark, and
removes all punctuation (equivalent to Worked exampleWrite a Sed command to remove all whitespace that terminates
lines.
Although it is most common for simple Sed commands to be applied to all lines of the input, you should also be familiar with being able to specify addresses of lines. Sometimes an editing problem can be solved either by a complex edit on every line of input or by a simple edit on only some of the input lines - the latter approach is preferable. Worked exampleWrite a filter to precede each word in
It is usual for Sed to be an element of a pipeline but, unlike tr, Sed can take a filename as argument, in which case the input will come from that file. So another solution would be:
If an ampersand (
If you want an actual ampersand to occur in the replacement string, it must be escaped by preceding it with a backslash. If you give sed '' sed -n 'p' See what happens if you have just: sed 'p' We can use sed '15p' will display line 15 of the input only, and sed '1,10s/[:alpha:]//g' will display the first ten lines only, with all letters deleted.
By using option
Worked exampleWrite a filter to display the last line of the input prepended
by
|
Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck