There are two metacharacters for specifying that a string appears at the beginning or end of a line. The caret (^) is the regular expression that indicates the start of the word. The dollar sign ($) is a regular expression that indicates a single character at the end of a line. These are often referred to as "locators," because they limit the match to a specific location. For example, you can use the following expression to print a line starting with first:
^first
Without the ^ metacharacters, this expression prints any line that contains first.
In general, use VI to enter text to be processed by troff, and you do not want a space to appear at the end of the line. If you want to find (and delete) them, the following regular expression can match the line with one or more spaces at the end:
--*$
Troff requests and macros must be entered at the beginning of the row. They are a two-character string preceded by a period. If a request or macro has a parameter, it usually follows a space. The regular expression used to search for such a request is:
^\... -
This expression matches the line with a period at the beginning of the row, followed by a two-character string, and then a space.
You can use two contiguous positional metacharacters to match a blank line, that is: ^$
You can use this mode to calculate the number of empty lines in a file, using the Count option-C in grep:
$ grep-C' ^$ 'Ch04
5
This regular expression is useful if you want to use SED to delete empty rows. The following regular expression can be used to match a blank line, even if it contains spaces:
^-*$
Similarly, the following expression can be used to match an entire row: ^.*$
In sed (and grep), it is only special if "^" and "$" appear respectively at the beginning or end of the regular expression. So "^abc" means "match only the letters A, B, and C at the beginning of the line, and" ab^c "means" match A, B, character ^, and then C "anywhere in the line. This is also used for "$".
Unlike awk, "^" and "$" are always special, even though they may make regular expressions written that do not match anything. It can be said that, in awk, when you want to match the character "^" or "$", it should be escaped with a backslash, regardless of where it is in the regular expression.
Reference: http://www.linuxawk.com/communication/441.html
Linux Regular Expressions-anchor metacharacters