Editor Basics


A sed program consists of one or more lines with the following syntax:

[address[,address]]instruction [argument-list]


The addresses are optional. If you omit the address, sed processes all lines from the input. The instruction is the editing instruction that modifies the text. The addresses select the line(s) that the instruction part of the command operates on. The number and kinds of arguments in the argument-list depend on the instruction. If you want to put several sed commands on one line you can separate the commands with semicolons (;).

The sed utility processes input as follows:

1.

Reads one line of input from file-list or standard input.

2.

Reads the first instruction from the program or program-file. If the address(es) select the input line, acts on the input line as the instruction specifies.

3.

Reads the next instruction from the program or program-file. If the address(es) select the input line, acts on the input line (possibly modified by the previous instruction) as the instruction specifies.

4.

Repeats step 3 until it has executed all instructions in the program or program-file.

5.

Starts over again with step 1 if there is another line of input; otherwise, it is finished.

Addresses

A line number is an address that selects a line. As a special case, the line number $ represents the last line of input.

A regular expression (refer to Appendix A) is an address that selects those lines containing a string that the regular expression matches. Although slashes are often used to delimit these regular expressions, sed permits you to use any character other than a backslash or NEWLINE for this purpose.

Except as noted, zero, one, or two addresses (either line numbers or regular expressions) can precede an instruction. If you do not use an address, sed selects all lines, causing the instruction to act on every line of input. Providing one address causes the instruction to act on each input line that the address selects. Providing two addresses causes the instruction to act on groups of lines. The first address selects the first line in the first group. The second address selects the next subsequent line that it matches; this line is the last line in the first group. If no match for the second address is found, the second address points to the end of the file. After selecting the last line in a group, sed starts the selection process over again, looking for the next line that the first address matches. This line is the first line in the next group. The sed utility continues this process until it has finished going through the entire file.

Instructions

d

(delete) The Delete instruction causes sed not to write out the lines it selects and not to finish processing the lines. After sed executes a Delete instruction, it reads the next input line and begins over again with the first instruction from the program or program-file.

n

(next) The Next instruction writes out the currently selected line if appropriate, reads the next input line, and starts processing the new line with the next instruction from the program or program-file.

a

(append) The Append instruction appends one or more lines to the currently selected line. If you precede an Append instruction with two addresses, it appends to each line that is selected by the addresses; earlier versions of sed did not accept Append instructions with two addresses. If you do not precede an Append instruction with an address, it appends to each input line. An Append instruction has the following format:

[address[,address]] a\ text\ text\ ... text


You must end each line of appended text, except the last, with a backslash (the backslash quotes the following NEWLINE). The appended text concludes with a line that does not end with a backslash. The sed utility always writes out appended text, regardless of whether you use a -n flag on the command line. It even writes out the text if you delete the line to which you are appending the text.

i

(insert) The Insert instruction is identical to the Append instruction except that it places the new text before the selected line.

c

(change) The Change instruction is similar to Append and Insert except that it changes the selected lines so that they contain the new text. When you specify an address range, Change replaces the entire range of lines with a single occurrence of the new text.

s

(substitute) The Substitute instruction in sed is similar to that in vim (page 172). It has the following format:

[address[,address]] s/pattern/replacement-string/[g][p][w file]


The pattern is a regular expression (refer to Appendix A) that is delimited by any character other than a SPACE or NEWLINE; traditionally a slash (/). The replacement-string starts immediately following the second delimiter and must be terminated by the same delimiter. The final (third) delimiter is required. The replacement-string can contain an ampersand (&), which sed replaces with the matched pattern. Unless you use the g flag, the Substitute instruction replaces only the first occurrence of the pattern on each selected line.

The g (global) flag causes the Substitute instruction to replace all nonoverlapping occurrences of the pattern on the selected lines.

The p (print) flag causes sed to send all lines on which it makes substitutions to standard output. This flag overrides the -n option on the command line.

The w (write) flag is similar to the p flag but it sends its output to the file specified by file. A single SPACE and the name of the output file must follow a w flag.

p

(print) The Print instruction writes the selected lines to standard output, writing the lines immediately, and does not reflect the effects of subsequent instructions. This instruction overrides the -n option on the command line. (The -n option prevents sed from copying lines to standard output.)

w file

(write) This instruction is similar to the Print instruction except that it sends output to the file specified by file. A single SPACE and the name of the output file must follow a Write instruction.

r file

(read) The Read instruction reads the contents of the specified file and appends it to the selected line. A single SPACE and the name of the input file must follow a Read instruction.

q

(quit) The Quit instruction causes sed to terminate immediately.

Control Structures

!

(NOT) Causes sed to apply the following instruction, located on the same line, to each of the lines not selected by the address portion of the instruction. For example, 3!d deletes all lines except line 3 and $!p displays all lines except the last.

{}

(group instructions) When you enclose a group of instructions within a pair of braces, a single address (or address pair) selects the lines on which the group of instructions operates. Use semicolons (;) to separate multiple commands appearing on a single line.

Branch instructions

The GNU sed info page lists the branch instructions as "Commands for sed gurus" and suggests that if you need them you might be better off writing your program in awk or Perl.

: label

Identifies a location within a sed program. The label is useful as a target for the b and t branch instructions.

b [label]

Unconditionally transfers control (branches) to label. Without label, skips the rest of the instructions for the current line of input and reads the next line of input (page 643).

t [label]

Transfers control (branches) to label only if a Substitute instruction has been successful since the most recent line of input was read (conditional branch). Without label, skips the rest of the instructions for the current line of input and reads the next line of input (page 643).

The Pattern Space and the Hold Space

The sed utility has two buffers. The commands reviewed up to this point work with the Pattern space, which initially holds the line of input that sed just read. The Hold space can hold data while you manipulate data in the Pattern space; it is a temporary buffer. Until you place data in the Hold space it is empty. This section discusses commands that move data between the Pattern space and the Hold space.

g

Copies the contents of the Hold space to the Pattern space. The original contents of the Pattern space is lost.

G

Appends a NEWLINE and the contents of the Hold space to the Pattern space.

h

Copies the contents of the Pattern space to the Hold space. The original contents of the Hold space is lost.

H

Appends a NEWLINE and the contents of the Pattern space to the Hold space.

x

Exchanges the contents of the Pattern space and the Hold space.




A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net