The grep egrep command and regular expression of Linux learning

Source: Internet
Author: User
Tags egrep

Fortunately in the Marco class to learn Linux, Margo told us to do the best, hope that through these blog to their own learning experience and content of a good record down, also is a review.

Grep:global search REgular expression and Print out of the line.

The text Search tool, which filters the target text according to the user-specified mode (pattern), displays the rows to which the pattern is matched.

grep [OPTION] ... ' PATTERN ' FILE ...
--color

Regular expression: A pattern written by a class of characters, some of which do not represent the literal meaning of a character, but rather a function of control or wildcard;
Metacharacters

Two categories:
Basic Regular Expressions
Extending regular Expressions

Basic Regular Expressions:
Character Matching:
.: Matches any single character
[]: 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:]], white space character
[[:p UNCT:]], punctuation
[^]: matches any single character outside the specified set
Number of matches: used to limit the number of occurrences of a character immediately preceding it
*: Match its preceding character any time, 0,1 or multiple times;
For example: grep ' X*y '
XY, XXY, Xxxy, y
\?: matches the preceding character 0 or 1 times;
For example: grep ' X\?y '
XY, Xxy, y, Xxxxxy, Aby
\+: Matches its preceding character to appear at least 1 times;
\{m\}: Matches the preceding character m times;
For example: grep ' X\{2\}y '
XY, Xxy, y, Xxxxxy, Aby
\{m,n\}: Matches its preceding character at least m times, up to n times;
For example: grep ' X\{2,4\}y '
XY, Xxy, y, Xxxxxxy, Aby
grep ' X\{0,4\}y '
XY, Xxy, y, Xxxxxxxxxy, Aby
grep ' X\{2,\}y '
XY, Xxy, y, Xxxxxy
. *: Matches any character of any length
Location anchoring:
^: Anchor at the beginning of the line
Write on the leftmost side of the pattern
$: End of line anchoring
Write at the far right of the pattern
^$: Blank Line

\<: The first anchor of the word, or \b
The left side of the word pattern to find out now; \<char
\>: Ending anchoring, or \b
The right side of the word pattern that you want to find now;char\>
\<pattern\>: Match words
Group: \ (\) matches multiple characters at the same time

For example: ' Ab\{1,3\}x ' can match ABX ABBX ABBBX

' \ (ab\) \{1,3\}x ' can match ABX ABABX ABABABX

Back reference: In the pattern, if you use \ (\) to implement the grouping, in a text check, if \ (\) pattern matches to a certain content, this content can be referenced in the following pattern;
\1, \2, \3
The pattern is from left to right, referencing the first # opening parenthesis and matching the pattern between the closing parenthesis and its matching right;

grep options:
-V: Reverse Selection
-O: Show only matches to content
-I: Ignore character case
-E: Using extended regular expressions
-A #: lines matching to rows down show # lines
-B #: lines matched to rows down show # lines
-C #: Matching lines up and down show # lines

To extend the regular expression:
and the basic regular expression is similar, generally in the match without the escape character \, only a few specific needs and escape
*,?, +,{m},{m,n},^,$,\<,\>,\1,\2
Condition Selection: a|b


Practice:

1. Display the lines in the/proc/meminfo file that begin with uppercase or lowercase s;

650) this.width=650; "title=" QQ picture 20141123160258.jpg "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M02/53/D7 /wkiom1rybo7hlvyoaaemgsdzako520.jpg "alt=" Wkiom1rybo7hlvyoaaemgsdzako520.jpg "/>

2. Display the user whose default shell is non-/sbin/nologin in the/etc/passwd file

650) this.width=650; "title=" QQ picture 20141123160527.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M00/53/D5 /wkiol1rybwybva6oaag4wuwzzoy635.jpg "alt=" Wkiol1rybwybva6oaag4wuwzzoy635.jpg "/>

3. Display the user whose default shell is/bin/bash in/etc/passwd file;
And the user whose ID number is the highest in the above results;

650) this.width=650; "Width=" 897 "height=" 142 "title=" QQ picture 20141123163507.png "Style=" width:705px;height:223px;float : none; "src=" Http://s3.51cto.com/wyfs02/M00/53/D7/wKiom1RyBO-QRfjuAAGdvGiMB_g193.jpg "alt=" Wkiom1rybo-qrfjuaagdvgimb_g193.jpg "/>

4. Identify the user whose user name and default shell are the same on the current system

650) this.width=650; "Width=" 564 "height=" 137 "title=" QQ picture 20141123222155.jpg "Style=" width:568px;height:140px;float : none; "src=" Http://s3.51cto.com/wyfs02/M01/53/D5/wKioL1RyBW6gyoowAAENihiIgQE185.jpg "alt=" Wkiol1rybw6gyoowaaenihiigqe185.jpg "/>

5. Find one or two digits in the/etc/passwd file;

650) this.width=650; "title=" QQ picture 20141123222739.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M01/53/D7 /wkiom1rybplhrb48aamxu-qfgas665.jpg "alt=" Wkiom1rybplhrb48aamxu-qfgas665.jpg "/>

6, displays the line that starts with at least one whitespace character in/boot/grub/grub.conf;

650) this.width=650; "Width=" 857 "height=" 131 "title=" QQ picture 20141123223136.jpg "Style=" width:749px;height:139px;float : none; "src=" Http://s3.51cto.com/wyfs02/M02/53/D5/wKioL1RyBXHyEfzkAAEWJ9RV0Ck234.jpg "alt=" Wkiol1rybxhyefzkaaewj9rv0ck234.jpg "/>

7, display the/etc/rc.d/rc.sysinit file, start with #, followed by at least one white space character, and then have at least one non-whitespace character line;

650) this.width=650; "title=" QQ picture 20141123225102.jpg "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M00/53/D5 /wkiol1rybxszbggoaajucngnxki650.jpg "alt=" Wkiol1rybxszbggoaajucngnxki650.jpg "/>


The grep egrep command and regular expression of Linux learning

Related Article

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.