The syntax of the
sed
command comes in two forms, as
sed [ -an ] command [file ] sed [ -an ] [-e command] [ -f command_file] [file ]
You will use one form or the other, but not both, in one command. The first form is the simpler one: one sed command is used to edit the input file(s). If no input files are used, sed edits data read from stdin. Here is a command of this type:
sed '/A/d' goodoleboys.txt
The
sed
command is /A/d. The input file is goodoleboys.txt, and the modified data is written to
You need to use the second form when you want to apply more than one editing command to the input. This type of command is shown in the following example:
sed -e 's/Daisy/Ethel/' -f seddata1.txt goodoleboys.txt
The
e
option allows you to include editing commands within the command string itself, while the
f
option
You can use the filter form of sed when you're doing interactive or scripted Qshell work. Sometimes, a sed solution is easier to write correctly than other forms of Qshell variable expansion and substitution.
Here is an example of sed as a filter:
for i in *.txt ; do j=$(echo $i sed -e 's/\.txt$/.new/'); echo mv $i $j; done
This example sends all text-file
The four options for sed are listed in Table 18.1.
|
Option |
Description |
|---|---|
|
a |
Delay opening of files to which output is directed with the w command. |
|
e |
Read a sed command from the following argument. |
|
f |
Read sed commands from the file named in the following argument. |
|
n |
Do not automatically write to
|
The a option delays the opening of files that are to be overwritten until the last possible moment. Normally, Qshell clears files that are to be overwritten before sed begins to run. This means that files will be cleared that might not be written to. The a option ensures that a file is not cleared unless it is written to.
The
e
option, which is repeatable, precedes a
sed
command. The
f
option, which is also repeatable, precedes
the
The
n
option
A
sed
command consists of three parts: the address, a
function, and arguments. You may precede the address and function
[address[,address]] function [arguments]
Let's look at each of the three parts in more detail.
The address identifies the lines to be selected. Depending on the function, you may specify no address, a single address, or two addresses separated from one another by a comma.
If you do not specify an address, all lines of the input file are selected for editing. If you specify one address, only the lines matching the address are edited. If you specify two addresses, sed edits one or more ranges of lines.
Each address can be
A line number, from all input files numbered consecutively
A dollar sign, to
A regular expression delimited by the forward-slash character, /
The regular expressions are similar to the basic regular expressions that grep and other utilities recognize, but sed adds two features of its own:
The escape sequence \n matches the newline character.
Any character other than a backslash or newline may be used as a
delimiter in regular expressions. Any
Table 18.2 lists the
|
Metacharacter |
Description |
|---|---|
|
(period) |
Match any character except end-of-line. |
|
* |
Match zero or more occurrences of the
|
|
^ |
Match from the beginning of the line. |
|
$ |
Match from the end of the line. |
|
[ ] |
Match any character within the brackets. Ranges may be specified with a hyphen. |
|
[^ ] |
Negate the groups or ranges of
|
|
\{m\} |
Match exactly m occurrences of the preceding character. |
|
\{m,\} |
Match m or more occurrences of the preceding character. |
|
\{m,n\} |
Match m to n occurrences of the preceding character. |
|
\ |
Turn off the special meaning of the following character. |
|
\(\) |
Define a back reference to save matched characters as a pattern. The saved pattern can be referenced with a backslash followed by a number. |
|
// |
Match the last-used regular expression. |
The function is the command itself. It
|
Function |
Arguments |
Description |
Maximum Addresses |
|---|---|---|---|
|
a |
text |
Write text to
|
1 |
|
b |
label (optional) |
Branch to a label. If a label is not specified, branch to the end of the list of functions. |
2 |
|
c |
text |
Replace line(s) with new text. |
2 |
|
d |
Do not write the pattern space to stdout. |
2 |
|
|
D |
Delete the pattern space up to and including the first newline character |
2 |
|
|
g |
Copy the holding buffer to the pattern space. |
2 |
|
|
G |
Append the holding buffer to the pattern space. |
2 |
|
|
h |
Copy the pattern space to the holding buffer. |
2 |
|
|
H |
Append the pattern space to the holding buffer. |
2 |
|
|
i |
text |
Write text to stdout before writing the pattern space. |
1 |
|
l(ell) |
Replace nonprintable characters with visual representations. |
2 |
|
|
n |
Write the pattern space to stdout (unless the-n option was
specified), and read the
|
2 |
|
|
N |
Append the next input line to the pattern space |
2 |
|
|
p |
Print the pattern space to stdout immediately. |
2 |
|
|
P |
Print the pattern space, up to and including the first newline character, immediately. |
2 |
|
|
q |
Terminate the editing session after processing the current input record. |
1 |
|
|
r |
file
|
Read a file into stdout. |
1 |
|
s |
search string, replacement string, flags |
Substitute one string for another. |
2 |
|
t |
Branch if substitutions have been made. |
2 |
|
|
w |
file name |
Write the pattern space to a file. |
2 |
|
x |
Exchange the contents of the holding buffer and the pattern space. |
2 |
|
|
y |
Replace each character in a set with the corresponding character of another set. |
2 |
|
|
= |
Write the line number to stdout. |
1 |
|
|
: (
|
label |
Define a label as a target for a branch. |
You may negate a function by preceding it with an exclamation point. The following example deletes all lines except line 2:
sed '2!d' goodoleboys.txt
You may also include more than one function for an address.
Enclose the
sed '2{h; d; }' goodoleboys.txt
When sed reads line 2, it executes two functions, h and d .
Instead of a semicolon and space, you may also separate functions with newline characters, like this:
sed '2{
>
h
>
d
>
}' goodoleboys.txt
As you can see, sed allows each function to be listed on its own line. When sed reads line 2, it executes two functions, h and d . Notice that the commands are separated by newline characters rather than by semicolons. The greater-than signs are the Qshell secondary prompt character.