Linux regular expression-positioning metacharacters and linux Regular Expressions
Two metacharacters are used to specify whether a string appears at the beginning or end of a row. A single character regular expression that indicates the start. The dollar sign ($) is a regular expression that indicates a single character at the end of a line. These are usually called "locators" because they limit matching to a specific location. For example, you can use the following expression to print rows starting with "First:
^ First
No ^ metacharacters. This expression prints any row containing the First character.
Generally, use VI to input the text to be processed by troff, and do not want spaces to appear at the end of a row. If you want to find and delete them, the following regular expression can match rows with one or more spaces at the end:
□□* $
Troff requests and macros must be input at the beginning of the row. They are two character strings with a period in front. If a request or macro has a parameter, it is usually followed by a space. The regular expression used to search for such a request is:
^ \... □
This expression matches "the line has a period at the beginning, followed by a string with two characters, followed by a space line ".
You can use two consecutive positioning metacharacters to match empty rows, that is, ^ $
You can use this mode to calculate the number of empty rows in the file. In grep, use the count option-c:
$ 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 empty rows, even if it contains spaces:
^ □* $
References: http://www.linuxawk.com/communication/441.html