32.5. Regular Expressions: The Anchor Characters ^ and $
Most Unix text facilities are line-oriented. Searching for patterns that span several lines is not easy to do. [But it is possible (Section 13.9, Section 11.10). -- JP] You see, the end-of-line character is not included in the block of text that is searched. It is a separator, and regular expressions examine the text between the separators. If you want to search for a pattern that is at one end or the other, you use anchors. The caret (^) is the starting anchor, and the dollar sign ($) is the end anchor. The regular expression ^A will match all lines that start with an uppercase A. The expression A$ will match all lines that end with uppercase A. If the anchor characters are not used at the proper end of the pattern, they no longer act as anchors. That is, the ^ is an anchor only if it is the first character in a regular expression. The $ is an anchor only if it is the last character. The expression $1 does not have an anchor. Neither does 1^. If you need to match a ^ at the beginning of the line or a $ at the end of a line, you must escape the special character by typing a backslash (\) before it. Table 32-1 has a summary.
Table 32-1. Regular expression anchor character examples
 Beware! If your regular expression isn't properly quoted, this means "process ID of current process." Always quote your expressions properly.
The use of ^ and $ as indicators of the beginning or end of a line is a convention other utilities use. The vi editor uses these two characters as commands to go to the beginning or end of a line. The C shell uses !^ to specify the first argument of the previous line, and !$ is the last argument on the previous line (Section 30.8 explains).
It is one of those choices that other utilities go along with to maintain consistency. For instance, $ can refer to the last line of a file when using ed and sed. cat -v -e (Section 12.5, Section 12.4) marks ends of lines with a $. You might also see it in other programs.
Copyright © 2003 O'Reilly & Associates. All rights reserved.