[grep Introduction:]
grep is used to filter rows that contain specific characters, and to use regular expressions to search for text that is selected in positive behavior units when looking up a string in the data.
Usage: grep [Cinvs] ' pattern ' file
For example: Grep-n ' BBB ' aaa.txt--color # searches for the keywords BBB from the file Aaa.txt and displays line numbers and highlights.
[Common options:]
-C: Only the count of matching rows is output.
-I: case-insensitive (applies to given only).
-N: Displays matching rows and line numbers.
-S: Do not display error messages that do not exist or do not match text.
-V: Displays all rows that do not contain matching text.
[Regular meta character:]
grep can be used in conjunction with regular expressions, and some of the commonly used regular expression filter characters are described below.
\: Escape character, ignoring the original meaning of the special character in the regular expression.
^: matches a line that starts with a string.
$: Matches the line ending with a string.
\<: Match the beginning of a word.
\>: Match the end of the word.
[]: Within [] a single character, such as [a], indicates that a meets the requirements.
[-]: a range character that is marked by [-], such as [A-z], that is, a, B, C, all the time until Z meets the requirements.
. : Indicates that there must be 1 arbitrary characters.
*: The preceding character can appear any time.
? : The preceding character appears 0 or 1 times.
+: The preceding character appears one or more times.
[Regular Example:]
1. ^ Represents the beginning of a line
grep ' ^bbb ' aaa.txt--color # finds a line beginning with BBB from the file aaa.txt.
2. $ indicates line end
grep ' bbb$ ' aaa.txt--color # finds the line at the end of BBB from the file aaa.txt.
3. Represents a single character that can match all characters except line breaks.
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/
grep ' bbb ... ' aaa.txt--color # from File Aaa.txt to find the three-character line in the back of the BBB, ' point ' can match the space.
4. * indicates that the character in front of it can appear any time
grep ' bbb.* ' aaa.txt--color # finds a line in the file aaa.txt that has any character following the BBB.
5. + indicates that the character in front of it must appear at least once
Grep-e ' bbb.+ ' aaa.txt--color # from File Aaa.txt find a line of at least one character behind the BBB.
6.? It means that the character in front of it can or may not be
Grep-e ' BBBC ' aaa.txt--color # Find the line in the file aaa.txt that has BBB or BBBC.
Note: grep does not support +,? These two metacharacters, if you want to use, can only use extended grep (Egrep or GREP-E)
7. [] to match a character, the characters appearing in [] are all or relationships
grep ' ^[bb]bb ' aaa.txt--color # searches the file aaa.txt for rows beginning with BBB or BBB.
grep ' [0-9] ' aaa.txt--color # finds rows containing numbers from file Aaa.txt
Grep-p ' \d ' aaa.txt--color # Find rows with numbers from File Aaa.txt,-p indicates regular expressions that use Perl
\d represents a number, \d represents a non-numeric
\s represents a space or tab, \s indicates that it is not a space or tab
\w represents any character (uppercase and lowercase letters, numbers, underscores), and \w is not a letter, number, or underscore.
Note: ^ appearing outside [] means ' opening ', if it appears in [] it means ' negation '
grep ' ^[^bb] ... ' aaa.txt--color # from the file aaa.txt look for rows that do not start with B or B, and that are followed by two characters.
8. \< match the beginning of a word
grep ' \<tom ' aaa.txt--color # finds the line from the file aaa.txt that begins with Tom.
9. \> match the end of the word
grep ' \>tom ' aaa.txt--color # finds a line in the file aaa.txt that ends the word with Tom.
10. Match a word, not a part of a string
grep ' \btom\b ' aaa.txt--color # finds the line containing the word tom from the file Aaa.txt, if it's atomb or something.
11. Use \ (\) to label, after the reference to use \ n is a number, \1 said the first to be caused by
grep ' \ (tom\) ... \1 ' Aaa.txt--color # find Tom from the file Aaa.txt, and it has three characters behind it, followed by a Tom's line. such as: Tomxxxtom
\{n\} indicates that the preceding character will appear n times
grep ' tomx\{3\} ' aaa.txt--color # find tomx from File Aaa.txt, x to appear 3 times, such as: tomxxx
grep ' tomx\{3,\} ' aaa.txt--color # finds tomx from file Aaa.txt, x must appear at least three times, such as Tomxxx or tomxxxx
grep ' tomx\{3,4\} ' aaa.txt--color # finds tomx from file Aaa.txt, X appears 3 or 4 times, such as Tomxxx or tomxxxx
Author: csdn Blog zdp072