Text Search grep Knowledge points Summary

Source: Internet
Author: User

Text Search tool: grep, Egrep
Filters the target file according to user-specified mode, displaying the rows to which the pattern matches
grep [OPTION] ... ' PATTERN ' FILE ...
--color

The difficulty lies in how the pattern is written, and the pattern is mainly the application of regular expressions
Regular expression: A pattern written by a class of characters, where some characters do not represent the literal meaning of a character,
But a function of control or wildcard.
Metacharacters: *,? etc
Two categories:
Basic Regular Expressions
Extending regular Expressions

Basic Regular Expressions:
Character Matching:
.: Matches any single character
For example: grep--color ' r. T '/etc/passwd
Match to R start, T end, two-character line between

[]: matches any single character in the specified collection
[[:d igit:]],[0-9]
[[: Lower:]],[a-z]
[[: Upper:]],[a-z]
[[: Alpha:]],[a-za-z]
[[: Alnum:]],[0-9a-za-z]
[[: Space:]]
[[:p UNCT:]]
For example: grep--color ' abcd[[:d igit:]][[:d igit:]][0-9] ' test
Matches a line that starts with ABCD, followed by three digits.

[^]: matches any single character outside the specified set
For example: grep--color ' abcd[^[:d igit:]] ' test
grep--color ' abcd[^0-9] ' test
Matches a line that begins with ABCD and is not a number behind

Number of matches: used to limit the number of occurrences of a character immediately preceding it
*: Match any of its preceding characters, 0,1 or multiple times
For example: grep ' X*y '
Xy,xxy,xxxy,y

\?: matches the preceding character 0 or 1 times, where \ is the escape character, which is the transfer?
For example: grep ' X\?y '
Xy,xxy,y,xxxy,aby for XXY, it does match to XY, and the first X is ignored.

\+: Matches the preceding character appearing at least once
For example: grep ' X\+y '
Xy,xxy,xxxy

\{m\}: Matches the preceding character m times
For example: grep ' X\{2\}y '
Xxy,xxxy

\{m,n\}: Matches the preceding character at least m times, up to N times
For example: grep ' X\{2,4\}y '
XXY, Xxxy
grep ' X\{2,\}y '
Xxy,xxxy

. *: Matches any character of any length

Location anchoring:
^: Anchor at the beginning of the line
Write on the leftmost side of the pattern
For example: grep--color ' ^ABCD '/etc/passwd
Match lines that begin with ABCD

$: End of line anchoring
Write at the far right of the pattern
For example: grep--color ' [0-9]$ '/etc/passwd
Match lines that end with a number

^$: Blank Line
For example: grep--color ' ^$ '/etc/passwd
Match Blank Lines

\&lt: initial anchor, \b, \ for escape character
To the left of the word pattern that you are looking for, \<char
For example: grep--color ' \<r '/etc/passwd
Match words that begin with R

\>: Ending anchor, \b,\ as escape character
The right side of the word pattern that you want to find now,char\>
For example: grep--color ' tor\> '/etc/passwd
Match words ending with Tor

\<pattern\>: Match words
For example: grep--color ' \<root\> '/etc/passwd
Match Word Root

Group:
\(\)
Back reference: In the pattern, if you use \ (\) to implement the grouping, in the examination of a line of text,
If the \ (\) pattern matches a certain content, the pattern behind this content can be referenced;
\1,\2,\3
Pattern from left to right, referencing the # opening parenthesis and matching the pattern between the closing parenthesis and its match

For example: grep--color ' ab\{1,\}y ' test
At least once b between a and Y
grep--color ' \ (ab\) \{1,\}y ' test
Before y at least once AB
grep--color ' \ (ab\) \{1,\}y\1 ' test
Before y at least once AB, and the first group is referenced behind
grep--color ' \ (ab\) \{1,\}y\2 ' test
Before y at least once AB, and a second group behind

grep options:
-V: Reverse selection, which shows lines outside the pattern match
Example: Grep-v--color ' ABCD ' test
Show lines outside of ABCD
-O: Show only what matches to
-I: Ignore character case
-N: Display line numbers when matched
-E: Using extended regular expressions
-A #: Match to the bottom # line
Example: Grep-a 2--color ' ABCD ' test
Display matching ABCD lines and their bottom two lines, total three rows
-B #: Match to the top # line
-C #: Match to the top and bottom # lines

Egrep and extended Regular expressions
Extending the meta-character of a regular expression
Character Matching:
.
[]
[^]

Match number limit:
*
?: match its preceding character 0 or 1 times
+: Match its preceding character at least 1 times
{m}: matches its preceding character m times
{M,n}:{m,},{0,n}

Anchoring:
^
$
\<, \>: \b

Group:
()
Support for back reference: \1, \2 ...

Or:
A|b:a or B
AB|CD:
Example: Egrep--color ' ab|c ' test
grep--color ' ab\|c ' test
Match a line containing AB or C
grep--color ' a\ (b\|c\) ' Test
Match a line containing AB or AC

Example: Grep-e ' PATTERN ' FILE ...
Egrep ' PATTERN ' FILE ...

Text Search grep Knowledge points Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.