1 , grep: Searches for text based on patterns and displays lines of text that conform to patterns
Text Word wildcards regular the expression's meta-character combination to match the criteria
grep ' root '/etc/passwd Search the/etc/passwd file with the root character of the line displayed
- I.: Show only rows that are matched by a pattern
--color : Rows with pattern matching are displayed in color
- v : Only rows that are not matched by the pattern are displayed
- o : Displays only strings that are matched by the pattern
2 , regular expressions:
Metacharacters
.: indicates matching any single character
grep ' R.. T '/etc/passwd
[]: matches any single character within the specified range
[^]: represents any single character outside the specified range
Number of matches (greedy mode): (greedy mode means to match the pattern as long as possible)
* : Represents any time that matches its preceding character
a*b It means a appears, followed by a B a,b,ab,aab,acb,amnb at any one time, and the last three is not possible.
.* : Matches any character of any length
a.*b Represents a beginning B end of the middle is anything can a,b,ab,aab,acb,amnb front two is not possible
\?: represents 1 or 0 times the character that matches the preceding
\{m,n} : Indicates that the character before it matches at least m times up to n times (\ is used as escape to avoid {} being interpreted by the shell)
\{1,\} means at least once more. Unlimited \{0,3\} means up to 3 times less unlimited
grep ' a.\{1,3\}b ' file Represents a 1-character 2-character 3-character occurrence between A and b any of them can be, but at least 1 appear at most 3
Location anchoring:
^ : Anchor the beginning of the line any content after this character must appear at the beginning of the line
$ : Any content preceding this character in the end of the anchor line must appear at the end of the row
grep ' w$ '/etc/inittab search for lines ending in W in/etc/inittab
^$ : Blank line
grep^$/etc/inittab | Wc-l
\< or \b: The anchor word indicates that any character following it must appear as a word header
\> or \b: An anchor ending indicates that any character following it must appear as the tail of the word
\(\): Grouping
grep "\ (ab\) *" File Indicates that there is a line of AB in the file and no ab line appears.
3 , grep: A command to filter text using a pattern defined by a basic regular expression (default)
-i-v-o--color are basic regular Expressions
- e : Using an extended regular expression
4 , extending the regular expression:
Extending the meta-character of a regular expression
Character Matching:
.: indicates matching any single character
[^]: represents any single character outside the specified range
[]: matches any single character within the specified range
This article is from the "TT Melbourne TT" blog, reproduced please contact the author!
A detailed description of the Linux system grep and regular expressions