Description of Linux regular expressions and extended expressions, egrep commands (August 27 job exercise)

Source: Internet
Author: User
Tags expression engine egrep

1. Summarize the use of the commands involved in this course and show the relevant examples;

Rights Management:
Rights Management: chmod
Affiliation Management: Chown, CHGRP
File Matte Code: Umask

Modify file Permissions: chmod
chmod [OPTION] ... Mode[,mode] ... FILE ...
chmod [OPTION] ... Octal-mode FILE ...
chmod [OPTION] ...--reference=rfile FILE:

Three categories:
U:owner
G:group
O:other
A:all

Two ways to use:
=mode
=rx, =r, =
Ug=rx, go=
[+|-]mode
U+r,
+w
+x

-r:recursive

Modified file owner: Chown
chown [OPTION] ... [OWNER] [: [GROUP]] FILE ...
chown [OPTION] ...--reference=rfile FILE ...
-r:recursive

Modified file group: CHGRP

Umask: Masking Code
Catalog: 777-umask
Files: 666-umask
If the permissions of a certain class of users have permission to execute, it will automatically add 1 to its permission bit.

Umask umask
Set: Valid only for the current shell process;

2. Summarize basic regular expressions and extended regular expressions

Grep:
Linux Text Processing Three musketeers:
grep: Text filtering tool;
Sed: Text editor (line); stream editor
awk: Text report Generator; awk implementation on Linux is gawk

Grep:global search REgular expression and Print out of the line.
Function: Text Search tool, according to user-specified "pattern" line to search for target text, print the matching line;
Pattern: The filter condition written by metacharacters and text characters of regular expressions;

Metacharacters: A character does not represent its literal meaning, but is used to denote a wildcard or control function;

Divided into two categories:
Basic Regular Expressions: BRE
Extended Regular expression: ERE

The regular expression engine;

grep [OPTIONS] PATTERN [FILE ...] :
Options:
--color=auto: The matching to the string to do highlighting;
-V: The display mode does not match the row;
-I: ignore character case;
-O: Displays only strings that can be matched to the pattern;
-Q: Silent mode;
-E: Using extended regular expressions;

The metacharacters of the basic regular expression:
Character Matching:
. : matches any single character;
[]: matches any single character within the specified range;
[^]: matches any single character outside the specified range;

Character Set:

POSIX Character Set Collection

Description

[: Alnum:]

Any one letter or number (A-Z, A-Z, 0-9)

[: Alpha:]

Any one letter (A-Z, A-Z)

[: ASCII:]

Any one ASCII range character (\x00–\x7f)

[: Cntrl:]

Any one control character (\x00–\x1f, \x7f)

[:d Igit:]

Any one number (0–9)

[:p rint:]

Any one of the ASCII characters that can be displayed (\x20–\x7e)

[: Space:]

Any one whitespace character (\x09–\x0d, \x20)

[: Graph:]

Any one of the ASCII characters that can be displayed, without spaces (\x21–\x7e)

[: Lower:]

Any one lowercase letter (a–z)

[: Upper:]

Any one capital letter (a–z)

[:p UNCT:]

can display the character [:p rint:] to remove the alphanumeric [: alnum:]

[: Xdigit:]

Any one hexadecimal digit (0-9, a-f, a-f)

[: Blank:]

Spaces or tabs (\x20, \x09)

Description When [: is followed by ^ , the character that represents the corresponding character set is not matched.

Number of occurrences: used to specify the number of times after the character;
*: any time;
Abxy
Xay
Xxxxxxxy

grep "X*y"

\?:0 or 1 times;
grep "X\?y"

\+:1 or multiple times;
\{m\}: Precisely limited to M times;
\{m,n\}: At least m times, up to n times, [M,n]
\{0,n\}: Up to n times;
\{m,\}: at least m times;

. *: Matches any character of any length;

Location anchoring:
^: Anchor at the beginning of the line, for the leftmost mode;
$: End of line anchoring; for the rightmost side of the pattern;
\< \b: The first anchor of the word; the left side of the pattern used to denote the word;
\> \b: the ending anchor; the right side of the pattern used to represent the word;
^$: blank line;

Group: \ (\)

The contents of the pattern in the grouped parentheses are recorded by the regular expression engine during execution, and the built-in variables are saved: The variables are \1, \2, ...
\1: From the left, the first opening parenthesis, and the matching right parenthesis in the middle of the pattern match to the content;
\2:
...

Back reference: Use a variable to refer to the character that matches the pattern in the preceding grouping brackets;

An extended regular expression:
The grep family has three commands:
grep: basic Regular expression
-e: extending regular expressions
-F: Regular expressions are not supported
Egrep: Extending Regular expressions
Fgrep: Regular expressions are not supported

Extend the metacharacters of regular expressions:

differs from the basic regular expression:

Number of matches:
*
?: 0 or 1 times;
+: more than 1 times;
{m}: exact match m times;
{M,n}: At least m times, up to n times;

Group: ()
Back reference: \1, \2, ...

Or:
A|b
C|cat: does not represent cat or cat, but is C or cat;
To be written (C|c) at

3. Display the lines in the/etc/passwd file that end with bash

[[email protected] ~]# grep ' \ (bash\) $ '/etc/passwd

Effect:

650) this.width=650; "title=" image "style=" border-right-width:0px;background-image:none;border-bottom-width:0px; padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px; "border=" 0 "alt=" image "src=" http:// S3.51cto.com/wyfs02/m01/72/56/wkiom1xhjrar2muhaae_djho51c425.jpg "width=" 642 "height=" 216 "/>

4. Display the two-digit or three-digit number in the/etc/passwd file

[[email protected] ~]# grep ' \b[0-9]\{2,3\}\b '/etc/passwd

Or

[Email protected] ~]# grep-e ' \b[0-9]{2,3}\b '/etc/passwd

Effect:

650) this.width=650; "title=" image "style=" border-right-width:0px;background-image:none;border-bottom-width:0px; padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px; "border=" 0 "alt=" image "src=" http:// S3.51cto.com/wyfs02/m02/72/56/wkiom1xhjruydb4eaakzai6auke414.jpg "width=" 726 "height=" 376 "/>

Attention:

[[email protected] ~]# grep ' [0-9]\{2,3\} '/etc/passwd wrong wording

5. Display lines with ' LISTEN ' followed by 0, one or more whitespace characters in the ' Netstat-tan ' command result

[Email protected] ~]# Netstat-tan | grep ' Listen[[:space:]] *$ '

Effect:

650) this.width=650; "title=" image "style=" border-right-width:0px;background-image:none;border-bottom-width:0px; padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px; "border=" 0 "alt=" image "src=" http:// S3.51cto.com/wyfs02/m00/72/53/wkiol1xhmmhiyghraaeonb9dces528.jpg "width=" 833 "height=" 290 "/>

6. Add user bash, Testbash, basher, and Nologin user (Nologin user's shell is/sbin/nologin), then find the same line in the/etc/passwd file as the user name and its shell name

[Email protected] ~]# egrep ' (\b[a-z]+[a-z]\b). *\1$ '/etc/passwd

Description: * Previous matches 0 or n times, \1 references the first \ (\) in front of the $ to what end

Effect:

650) this.width=650; "title=" image "style=" border-right-width:0px;background-image:none;border-bottom-width:0px; padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px; "border=" 0 "alt=" image "src=" http:// S3.51cto.com/wyfs02/m01/72/53/wkiol1xhmmlhqsa3aafek2ox718828.jpg "width=" 925 "height=" 261 "/>

7. Display the default shell and UID of root, CentOS, or User1 user on the current system (please create these users beforehand, if not present)

[[email protected] ~]# egrep ' ^ (root|centos|user1) '/etc/passwd | Cut-d:-f3,7

Effect:

650) this.width=650; "title=" image "style=" border-right-width:0px;background-image:none;border-bottom-width:0px; padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px; "border=" 0 "alt=" image "src=" http:// S3.51cto.com/wyfs02/m02/72/53/wkiol1xhmmswagffaacl8tgu98u098.jpg "width=" 943 "height=" 215 "/>

8, find a word in the/etc/rc.d/init.d/functions file (the middle of the word can be underlined) followed by a set of parentheses line

[[email protected] ~]# egrep ' ^ (\w+) \ (. *\) '/etc/rc.d/init.d/functions//This effect is immediately followed by a word ()

[[email protected] ~]# egrep ' ^\w+.*\ (. *\) '/etc/rc.d/init.d/functions//This effect is not followed by a word (), but there is (), and does not end with ()

Effect:

650) this.width=650; "title=" image "style=" border-right-width:0px;background-image:none;border-bottom-width:0px; padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px; "border=" 0 "alt=" image "src=" http:// S3.51cto.com/wyfs02/m00/72/53/wkiol1xhmmahgocqaaefjod_ric748.jpg "width=" 966 "height=" 385 "/>

9, use echo to output a path, and then egrep find its path base name; Further use Egrep to remove its directory name

Path base Name
[Email protected] ~]# echo "/etc/sysconfig/modules/" | Egrep ' [^/]+/$ '

Effect:

650) this.width=650; "title=" image "style=" border-right-width:0px;background-image:none;border-bottom-width:0px; padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px; "border=" 0 "alt=" image "src=" http:// S3.51cto.com/wyfs02/m01/72/53/wkiol1xhmmez0qzraab-bqzj1k0740.jpg "width=" 687 "height=" 165 "/>

Directory Name
[Email protected] ~]# echo "/etc/sysconfig/modules" | Egrep--color=auto ' (^[/].*[/]\b) '

Effect:

650) this.width=650; "title=" image "style=" border-right-width:0px;background-image:none;border-bottom-width:0px; padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px; "border=" 0 "alt=" image "src=" http:// S3.51cto.com/wyfs02/m02/72/53/wkiol1xhmmijso7waab36ued1ey006.jpg "width=" 682 "height=" 158 "/>

Description

[Email protected] ~]# basename/etc/sysconfig/modules/
Modules
[Email protected] ~]# dirname/etc/sysconfig/modules/
/etc/sysconfig
[Email protected] ~]#

10. Find the number between 1-255 in the result of ifconfig command execution

[Email protected] ~]# Ifconfig | Egrep-n ' \b[1-9]\b|\b[1-9][0-9]\b|\b[1-2][0-9][0-5]\b|\b[1][0-9][0-9] '

Effect:

650) this.width=650; "title=" image "style=" border-right-width:0px;background-image:none;border-bottom-width:0px; padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px; "border=" 0 "alt=" image "src=" http:// S3.51cto.com/wyfs02/m01/72/53/wkiol1xhmm2jjhr7aaiwj0ktpn8676.jpg "width=" "height=" 297 "/>

Description of Linux regular expressions and extended expressions, egrep commands (August 27 job exercise)

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.