The application of grep and regular expressions
Linux Text Processing Three musketeers:
grep: text filtering tools;
sed : Text editor (line); stream Editor
awk : Text report Generator; awk implementation on Linux is Gawk
grep : Global search REgular expression and Print out Theline.
Function: Text Search tool, according to user-specified "pattern" line to search for target text, print the matching line;
pattern : The filter condition written by metacharacters and text characters of regular expressions;
grep [OPTIONS] PATTERN [FILE ...] :
Options:
--color=auto : The matching string is highlighted;
- v : Displays the lines that the pattern does not match;
- I.: ignores character case;
- o: display only the string lines that can be matched to the pattern, and each match to a row, you can easily count the number of times;
- Q: silent mode;
- e : Using an extended regular expression;
The basic regular expression is divided into 2 categories:
Basic Regular Expressions: BRE
Extended Regular expression: ERE
Metacharacters of the basic Regular expression: (character does not represent its literal meaning, but is used to denote a wildcard or control function)
character matching:
.: match any single character;
[]: matches any single character within the specified range;
[^] : matches any single character within the specified range;
[: Lower:],[:upper:], ...
number of matches: used to specify the number of times the character behind, can only be used for the previous character;
*: any time;
Abxy
Xay
Xxxxxxxy
650) this.width=650; "title=" 1.png "src=" Http://s3.51cto.com/wyfs02/M02/72/56/wKiom1XhHy_QjaKBAAA3eXD90ws825.jpg " alt= "Wkiom1xhhy_qjakbaaa3exd90ws825.jpg"/>
\? : 0 or 1 times;
650) this.width=650; "title=" 2.png "src=" Http://s3.51cto.com/wyfs02/M00/72/52/wKioL1XhIWbAM5ckAABBoPHQZp0137.jpg " alt= "Wkiol1xhiwbam5ckaabbophqzp0137.jpg"/>
\+ : 1 or more times;
\{m\} : Precisely limited to M times;
\{m,n\}: at least m times, up to n times, [M,n]
\{0,n\} : Up to n times;
\{m,\} : at least m times;
650) this.width=650; "title=" 3.png "src=" Http://s3.51cto.com/wyfs02/M01/72/52/wKioL1XhIYGypVNyAAA8_mI3OXE559.jpg " alt= "Wkiol1xhiygypvnyaaa8_mi3oxe559.jpg"/>
.*: match any character of any length;
650) this.width=650; "title=" 4.png "src=" Http://s3.51cto.com/wyfs02/M01/72/52/wKioL1XhIZWyBzPBAABIaVFzbdY445.jpg " alt= "Wkiol1xhizwybzpbaabiavfzbdy445.jpg"/>
Location anchoring:
^: anchor at the beginning of the line, for the leftmost mode;
$: end of line anchoring; for the rightmost side of the pattern;
\<,\b: The first anchor of the word; the left side of the pattern used to denote words;
\>,\b : the ending anchor; the right side of the pattern used to represent the word;
^$: blank line;
Group: \ (\)
The contents of the pattern in the grouped parentheses are recorded by the regular expression engine during execution, and the built-in variables are saved: The variables are \1, \2, ...
\ 1: from the left, the first opening parenthesis, and the matching right parenthesis in the middle of the pattern match to the content;
\2 :
...
Back reference: Use a variable to refer to the character that matches the pattern in the preceding grouping brackets;
This article is from the "naïve Little Comrade" blog, please be sure to keep this source http://dengxi.blog.51cto.com/4804263/1689496
The application of-grep and regular expressions in the learning path of Linux