the Linux Family has three tools for text processing, such as grep, awk, and sed, and today I'll introduce the grep Text Processing tool.
First, what is grep?
grep is a text Search tool that filters the given text based on "pattern", checks the target text, and prints a text-processing tool that matches the conditional text. When it comes to pattern, what is pattern? That's what everyone knows about "regular expressions."
the grep Family has three other members: Grep,egrep and fgrep.
grep: supports regular expressions.
egrep: Supports the use of extended regular expressions.
Fgrep
grep :grep &NBSP, [OPTIONS] pattern
grep + options + Regular Expressions + text files
grep-i: ignore character case
~]# grep-i ' uuid '/tmp/fstabuuid=17caf342-d579-4dad-a16c-cf39353271c9/uuid=7ddc4cb3-07a9-47be-8273-afbded7b46e3/ Boot
Grep-o : Displays only the text that matches to itself
~]# grep-o ' ^uuid '/tmp/fstabuuiduuid
grep-v : Reverse Match
~]# grep-v "bash$"/etc/passwdbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin
Basic Regular expression meta-characters
character matching:
. : Matches any single character
~]# grep ' P. '/etc/passwdlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[]: matches any single character in the range:
~]# grep "[A]"/etc/fstab #/etc/fstab# Created by Anaconda on Sun Feb 28 11:21:11 2016
[^]: matches any single character outside of the range
When it comes to scope, simply introduce the special symbol:
[: Alpha:] : Represents any English letter (case insensitive) [:d Igit:]: Represents all numbers
[: Lower:]: represents all lowercase letters [: Upper:]: represents all uppercase letters
[: Alnum:]: represents all English case and number [: Space:]: Any character that produces whitespace
[:p UNCT:]: represent punctuation [: Blank:]: represents the space Bar and TAB key
If we want to take/etc/passwd text that is not a letter,
~]# grep "[^[:alpha:]]"/etc/fstab ##/etc/fstab#created by Anaconda on Sun Feb 28 11:21:11 2016
number of matches:
*: matches the preceding character any number of times (0,1 or more)
~]# grep "O*t"/etc/passwd Root:x:0:0:root:/root:/bin/bashshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
.* : Any character of any length
\+: Matches the preceding character at least 1 times
~]# grep "O\+t"/etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin
\{m\} : The preceding character appears m times and M is a non-negative integer
~]# grep "O\{2\}t"/etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin
Location anchoring:
^ : Anchors the beginning of the line, i.e. the leftmost $: Anchor line end, i.e. rightmost
~]# grep-i "^c"/etc/passwdcolord:x:998:996:user for Colord:/var/lib/colord:/sbin/nologinchrony:x:996:994::/var/lib /chrony:/sbin/nologin
The above is the beginning of C and the sentence at the beginning of the election.
The end-of-line anchor is the same as the use of the beginning of the anchor, but the inverse triangle is removed, and a $ ("x$") is added at the end of the row
Egrep :
What is Egrep? In fact, Egrep is the grep command that supports the use of extended regular expressions, which is grep-e.
egrep also has its own extended regular expression metacharacters
First character match:
. : Represents any single character
~]# Egrep ' r. T '/etc/passwdroot:x:0:0:root:/root:/bin/bashftp:x:14:50:ftp User:/var/ftp:/sbin/nologin
As long as there are r and T will be found, which is two characters, so the last line of R/ft will also be selected
[]: matches any single character within the range
~]# grep "[ABC]"/ETC/FSTAB #/etc/fstab# Created by Anaconda on Sun Feb 28 11:21:11 2016
As long as there is one of the ABC characters, it will be selected.
[^]: represents any character outside the matching range, using the same usage as the [] usage.
[ ] and [^] can both use the range of [: Alpha:][:lower:] And so on mentioned earlier.
number of matches:
*: matches any time
? : matches 0 or 1 times
+: match one or more times
The above three types are the same as the usage in grep, where the number of matches is stated:
{m}: match m times {m,n}: min. match m times, up to N times
{0,n}: matches up to n times {m}: minimum match m times
~]# egrep "o{1,3}"/etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin
The above is illustrated by the example of finding at least one o, up to three O.
Next is the location anchor:
^: Anchoring the beginning of the line $: Anchor Line End \<, \b: Anchor Word head \>, \b: Anchor ending
The anchor at the end of the beginning of the line is the same as the use of grep, where the anchor of the first ending is described here, as an example of the first anchor of the word.
~]# egrep "\<s"/etc/passwdbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin
The above is the regular expression of some basic usage, to skilled use of this expression to find this document, but also to distinguish when to use grep, when to use the Egrep, but also to practice a lot to master grep thoroughly.
This article is from the "11275479" blog, please be sure to keep this source http://11285479.blog.51cto.com/11275479/1750330
Linux Text Processing tool The Three Musketeers grep