home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


sed & awk

sed & awkSearch this book
Previous: A.1 Command-Line Syntax Appendix A
Quick Reference for sed
Next: A.3 Command Summary for sed
 

A.2 Syntax of sed Commands

Sed commands have the general form:

[address [,address ]][! ]command [arguments ]

Sed copies each line of input into a pattern space. Sed instructions consist of addresses and editing commands. If the address of the command matches the line in the pattern space, then the command is applied to that line. If a command has no address, then it is applied to each input line. If a command changes the contents of the space, subsequent command-addresses will be applied to the current line in the pattern space, not the original input line.

A.2.1 Pattern Addressing

address can be either a line number or a pattern , enclosed in slashes (/pattern /). A pattern is described using a regular expression. Additionally, \n can be used to match any newline in the pattern space (resulting from the N command), but not the newline at the end of the pattern space.

If no pattern is specified, the command will be applied to all lines. If only one address is specified, the command will be applied to all lines matching that address. If two comma-separated addresses are specified, the command will be applied to a range of lines between the first and second addresses, inclusively. Some commands accept only one address: a , i , r , q , and = .

The ! operator following an address causes sed to apply the command to all lines that do not match the address.

Braces ({}) are used in sed to nest one address inside another or to apply multiple commands at the same address.

[/pattern /[,/pattern /]]{
command1
command2
}

The opening curly brace must end a line, and the closing curly brace must be on a line by itself. Be sure there are no spaces after the braces.

A.2.2 Regular Expression Metacharacters for sed

The following table lists the pattern-matching metacharacters that were discussed in Chapter 3, Understanding Regular Expression Syntax .

Note that an empty regular expression "//" is the same as the previous regular expression.

Table A.1: Pattern-Matching Metacharacters
Special Characters Usage
. Matches any single character except newline .
*

Matches any number (including zero) of the single character (including a character specified by a regular expression) that immediately precedes it.

[...]

Matches any one of the class of characters enclosed between the brackets. All other metacharacters lose their meaning when specified as members of a class. A circumflex (^) as the first character inside brackets reverses the match to all characters except newline and those listed in the class. A hyphen (-) is used to indicate a range of characters. The close bracket (]) as the first character in the class is a member of the class.

\{n,m \}

Matches a range of occurrences of the single character (including a character specified by a regular expression) that immediately precedes it. \{n \} will match exactly n occurrences, \{n, \} will match at least n occurrences, and \{n,m \} will match any number of occurrences between n and m . (sed and grep only).

^

Locates regular expression that follows at the beginning of line. The ^ is only special when it occurs at the beginning of the regular expression.

$

Locates preceding regular expression at the end of line. The $ is only special when it occurs at the end of the regular expression.

\

Escapes the special character that follows.

\( \)

Saves the pattern enclosed between "\(" and "\)" into a special holding space. Up to nine patterns can be saved in this way on a single line. They can be "replayed" in substitutions by the escape sequences "\1" to "\9".

\n

Matches the n th pattern previously saved by "\(" and "\)", where n is a number from 1 to 9 and previously saved patterns are counted from the left on the line.

&

Prints the entire matched text when used in a replacement string.


Previous: A.1 Command-Line Syntax sed & awk Next: A.3 Command Summary for sed
A.1 Command-Line Syntax Book Index A.3 Command Summary for sed

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System