grep is a text retrieval tool, known as one of the Three Musketeers of Linux text processing, usually combined with regular expressions, support basic regular expressions and extended regular expressions, the function is very powerful, to our search text brought great convenience, by default, it is greedy mode, can match how long to match how long, The entire line that matches to is displayed. The other two great musketeers: sed, awk.
grep Common options:
Grep-i: Ignoring case
[email protected] ~]# grep-i tom/etc/passwd
Tom:x:506:506::/home/tom:/bin/bash
Tom:x:507:507::/home/tom:/bin/bash
Grep-o: Show only the text that matches to
[email protected] ~]# Grep-o root/etc/passwd
Root
Root
grep --color: Highlighting matched text
[Email protected] ~]# grep--color=auto tom/etc/passwd
Tom: x:506:506::/home/Tom:/bin/bash
grep-v: Display text other than matching text
[Email protected] ~]# grep-v tom/etc/passwd |tail-2
Named:x:25:25:named:/var/named:/sbin/nologin
Tom:x:507:507::/home/tom:/bin/bash
GREP-E: Represents a logical relationship "or"
[Email protected] ~]# grep-e tom-e tom/etc/passwd
Tom:x:506:506::/home/tom:/bin/bash
Tom:x:507:507::/home/tom:/bin/bash
Grep-q: Silent mode, no match to or not matched to text display
[Email protected] ~]# grep-q tom/etc/passwd
[Email protected] ~]#
grep-a #: Display mode matches to the text and displays after # lines
[Email protected] ~]# grep-a 1 named/etc/passwd
Named:x:25:25:named:/var/named:/sbin/nologin
Tom:x:506:506::/home/tom:/bin/bash
Grep-b #: Display pattern matches to the text and displays the front # lines
[Email protected] ~]# Grep-b 1 named/etc/passwd
Mage:x:505:505::/home/mage:/bin/bash
Named:x:25:25:named:/var/named:/sbin/nologin
Grep-c #: Displays the text to which the pattern matches, and displays the front and back # lines
[Email protected] ~]# grep-c 1 named/etc/passwd
Mage:x:505:505::/home/mage:/bin/bash
Named:x:25:25:named:/var/named:/sbin/nologin
Tom:x:506:506::/home/tom:/bin/bash
GREP-E: Support for extended regular expressions
grep is based on pattern matching, which supports basic regular expressions by default and supports extended regular expressions using the-e option, equivalent to Egrep.
Regular expressions have a number of meta-characters, with the basic regular expression Bre and the extended regular expression ere.
Basic regular Expression meta-character: BRE
Character Matching:
.: Matches any single character
"": matches any single character in the range
"^": match any single character outside the range TT
":d igit:"::: All Numbers ": Alnum:": All numbers and letters
": Lower:": All lowercase letters ": space:": white space characters
":p UNCT:": All Punctuation ": Alpha:": All Letters
": Upper:": All uppercase letters
[[email protected] ~]# grep t.m/etc/passwd
Tom:x:507:507::/home/tom:/bin/bash
[[email protected] ~]# grep [Tt]/etc/passwd |tail-2
Tom:x:506:506::/home/tom:/bin/bash
Tom:x:507:507::/home/tom:/bin/bash
number of matches:
*: matches the preceding character any time
. *: Matches any character of any length
\+: Matches the preceding character at least once
\? : matches the preceding character 0 or 1 times
\{m\}: Matches the preceding character m times
\{m,n\}: Matches the preceding character at least m times, up to N times
\{m,\}: Matches the preceding character at least m times
\{,n\}: Matches the preceding character up to n times
[[email protected] ~]# grep "X*y" Hadop
Xxy
Xy
YY matches the previous x any time, 0 times also matches
[[email protected] ~]# grep "X\+y" Hadop
Xxy
XY matches the previous X at least once
[[email protected] ~]# grep "X\{2\}y" Hadop
XXY matches the previous x two times, exact match
Location Anchoring:
^: Anchor at the beginning of the line
$: End of line anchoring
^$: Blank Line
^pattern$: For pattern matching entire row
^*$: Non-blank line
\< or \b: The first anchor of the word
\> or \b: Final anchoring
\<pattern\>: Anchoring the whole word
[[email protected] ~]# grep "^root"--color=auto/etc/passwd
Root:x:0:0:root:/root:/bin/bash
[[email protected] ~]# grep "/sbin/nologin$"/etc/passwd |head-2
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Group:
\ (\): One or more characters are bundled together and treated as a whole, and the contents of the grouping match are recorded in the internal variables by the regular expression engine, which are named: \1,\2,\3 .... You can make a back reference that references the character that the pattern in the preceding grouping matches to
\1: From the left, the first opening parenthesis and the character matching the pattern between the closing parenthesis.
\ (string1\+\ (string2\) *\)
\1:string1\+\ (string2\) *
\2:string2
[[email protected] ~]# grep "\ (r. t\). *\1 "--color=auto/etc/passwd
Root:x:0:0:root:/root:/bin/bash
The previous match to Root, recorded in the variable \1, after the reference, in the bank, in the presence of root will be pattern matching
[[email protected] ~]# grep "\ (r. t\) "--color=auto/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
Ftp:x:14:50:ftp user:/var/ftp:/sbin/nologin
If you only group, do not make a back reference, the result is so, as long as the group matching to will show
extended Regular Expression meta-character: ERE
Extended regular expression metacharacters are more concise than basic regular expressions, character matching, positional anchoring, and meta-characters are the same. Only the number of times matches and the grouping meta characters are slightly biased.
Number of matches:
*: matches the preceding character any time
. *{}: Matches any character of any length
+: Match the preceding character at least once
? : matches the preceding character 0 or 1 times
{m}: Matches the preceding character m times
{M,n}: matches the preceding character at least m times, up to N times
{, n}: matches the preceding character up to n times
{m,}: matches the preceding character at least m times
Group: ()
This article is from the "Linux" blog, so be sure to keep this source http://wei2008.blog.51cto.com/11351016/1795388
grep text-processing tool, regular expression