Linux-grep and regular expressions

Source: Internet
Author: User
The need for text lookup:
Grep,egrep,fgrep
grep: Searches the text according to the pattern, and displays the line of conformance to the pattern,
Pattern: A matching condition in which a literal character wildcards expressions a combination of the metacharacters of an expression
Format: grep [option] Pattern file ...
-I.
--color
-V: Shows rows that are not matched by a pattern
-O: Displays only strings that are matched to the pattern
-E: Using extended regular Expressions =egrep
-A N: After n rows of matching rows
-B N: before n rows matching rows
-C N: Matches the front and back n rows of a row
Compare the globbing you've learned before:
*,。 , [],[^]
Mans 7 Glob


Regular expression: Regular expression,regexp
Note: grep is a partial match of the entire row (the longest match), showing the entire row


Regular expression Categories:
1, basic REGEXP: Fundamental
2, Extended REGEXP: Expansion




Metacharacters
. Represents matching a single arbitrary character
[]: matches any single character within the specified range
[^]: matches any single character outside the specified range
Character set: [: space:][:d igit:],[:lower:] ...


Number of matches:
*: matches the preceding character any time
A*b
A,b,ab,aab




. *: Any character of any length
。 : matches the preceding character 1 or 0 times (typically with a translation character \. )
\{m,n\}: Matches the preceding character at least m times, at most n times


Position Anchor:
^: Anchor the beginning of the line, any content after this character must appear at the beginning of the line
$: Anchor line end, any content after this character must appear at the end of the row
Eg:grep ' ^a.*a$ ' Test.txtt #以a开头和结尾的行
^$: Blank Line


\< or \b: Any character that follows must appear as the header of the word
\> or \b: Any character preceding it must appear as the end of the word
Eg:\<root\&gt: The word must be root,rootroot or not.


Group:
\(\)
Eg:\ (ab\) *,ab can occur any time




Another important use of grouping is to implement a back-to reference:
\1: Referencing the first bracket grouping




Extended Regular Expression:
Character Matching:
.
[]
[^]


Number of matches:
*
?: no \
+: Matches the preceding character at least 1 times
{M,n}


Position Anchor:
^
$
\<
\>


Group:
(): Grouping
\1,\2,\3 ...


Or
|: Indicates or


Practice:
Match 1-255 of the numbers
Egrep ' \< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-9]) \> ' test.txt


Match 255.255.255.255 of a numeric string of this type
Egrep ' (\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-9]) \>\.) {3} (\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-9] \>\.) ' Test.txt


Why not be quoted later.


Fgrep: Regular expressions are not supported, but the execution speed is fast



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.