Regular expressions often contain shell metacharacters (such as s, *, etc.), and it is recommended to enclose regular expressions on merriness using single quotation marks (")
1. Line positioning symbols
Beginning of position symbol ^ and line tail Locator $
#找出以s开头的行:
' ^s ' /etc/passwd
#找出以h结尾的行:
' h$ ' /etc/passwd
2. Word locator (positioning words)
#找出包含以s开头的单词的行
' \<s ' /etc/passwd
#找出包含以h结尾的单词的行
' h\> ' /etc/passwd
In the same file, the number of rows found by word positioning is greater than the number of rows found at the beginning or end of the line:
' \<s ' /etc/passwd |WC-lnotoginseng'^s' /etc/passwd |WC- L5
3.grep Common Options
Options |
Function |
-I. |
Use the provided regular expression: But not case-sensitive |
-V |
Show only rows that do not contain regular expression matches |
-R |
A data search that recursively matches regular expressions is applied to a set of files or directories |
-A <number> |
Shows the number of rows after a regular expression match |
b <number> |
Shows the number of rows before a regular expression match |
-E |
If multiple-e options are used, multiple regular expressions can be provided and used with logical OR |
RHCE7--using regular expressions with Gerp