I. Introduction
Grep is used to display the part.) match the rows in string format.
Family: grep egrep fgrp
Ii. How to Use grep
Usage: grep [options] PATTERN file ..
Options:
-I case-insensitive
-- Color Matching highlighted
-O: only show Matching Parts
-N: displays the matched rows and rows.
-V Inversion
-E: use an extended regular expression.
-A n: displays the matched rows and the content of the last N rows.
-B N: displays the matched rows and the content of the N rows before the matched rows.
-C n: display the content of the matched rows and the content of the plus N rows before and after the matched rows.
Basic Regular Expression: REGularEXPression
① Metacharacters:
. Any single character
[] A single character in the specified range
[^] Except for a single character in the specified range
② Number of Characters
* Match any character before
. * Any character of any length
\? Match the first character 0 or 1 time
\ {M, n \} matches the previous character at least m times, at most n times
\ {3 \} matches the previous character three times
\ {1, \} matches the previous character at least once
③ Positioning
^ Beginning of Line
$ End of line
^ $ Blank line
\ <Or \ B any character after it must appear as the first part of the word
\> Or \ B, the character before it must end with the end of the word
\ <Root \> match the word root
Note: You need to add "" when using it.
④ Group
\ (Root \) takes root as a whole
\ (Root \) * root can appear as a whole for any number of times
Group backward reference:
\ (Root \). * \ 1 is equivalent\ (Root \). * \ (root \)
\ 1 reference content in the first one)
\ 2 reference content in second)
....
Extended regular expression:
Use grep-E or egrep
① Metacharacters:
Same as the basic Regular Expression
② Number of Characters
*
?
+ At least once
{M, n} is the same as a basic regular expression, but the escape symbol \
③ Positioning
^
$
\ <
\>
④ Tuples are basically the same as basic regular expressions, but only escape characters are missing)
()
\ 1
\ 2
⑤ Or "|"
Dog | cat dog or cat
Exercise: capture the IP address x. x in ifconfig)
Ideas: x [1-9] | [1-9] [0-9] | [1] [0-9] [0-9] | [2] [0-5] [0-5]
This article is from the keepalive blog, please be sure to keep this source http://jefferyyu.blog.51cto.com/2843635/1300245