Linux OPS Practice-August 27, 2015 Course assignments

Source: Internet
Author: User
Tags control characters expression engine egrep

Basic Regular Expression grep command

First, Summary:

grep, Egrep, Fgrep

Divided into two categories:

Basic Regular Expressions: BRE

Extended Regular expression: ERE

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

Function: The text Search tool, according to the user-specified "pattern" line to search for the target text, print the matching line; pattern: a matching condition that is a combination of metacharacters of the text wildcards regular the expression, which can be enclosed in single quotation marks To avoid the effects of shell wildcard, strong references do not replace and display the characters themselves. "" Double quotes, "in the string,", $, \ And so on, are replaced by the shell interpretation and then passed to grep. The normal string (a string without special characters and spaces) can also be searched without quotation marks.

Pattern: The filter condition written by metacharacters and text characters of regular expressions;

Syntax: grep [Options]pattern [FILE ...]

Common parameters:--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;

-C Displays the number of rows that are matched to

-N Output line number

-V reverse selection, which is to find rows without a search string

-W Match Word

-A # is displayed along with the next # line of the matching line, #代表任意数字

-B # is displayed along with the # line of the matching line, #代表任意数字

The-C # is displayed along with the top and bottom # lines of the matching row, #代表任意数字

-R or-R recursively searches for a matching word in the directory or subdirectory (can be combined with the Find command

-e equivalent to Egrep support for extended regular expressions

-F equivalent to Fgrep does not support regular expressions

Basic regular Expression meta-characters:

Character matching:.: matches any single character;

[]: matches any single character within the specified range;

[^]: matches any single character within the specified range;

POSIX special character class: Can be viewed through Man 7 glob

[: Alpha:] literal character [a-za-z]

[:d Igit:] numeric characters [0-9]

[: Graph:] non-null characters (not spaces, control characters)

[: Lower:] lowercase characters [A-z]

[: Upper:] Uppercase characters [A-z]

[: Alnum:] [0-9a-za-z]

[: Cntrl:] control character

[:p rint:] non-null characters (including spaces)

[:p UNCT:] Punctuation

[: space:] All whitespace characters (new line, Space, tab), tab can also be represented by \ t

[: xdigit:] hexadecimal digits and letters (0-9,A-F,A-F)

We use this when we are using: [[: Alpha:]], if the inverse is [^[:d igit:]]

Number of occurrences: used to specify the number of times after the character;

*: any time;

\?:0 or 1 times;

\+: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:

Character Matching:

.: Any single character

[]:

[^]:

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;


Anchoring:

^: Anchoring the beginning of the line

$: Anchor Line End

\<, \b

\>, \b

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


Second, practice

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

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/6C/wKiom1XjL9qQti6mAAEw052LYa0062.jpg "title=" 1.jpg " alt= "Wkiom1xjl9qqti6maaew052lya0062.jpg"/>

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

[[email protected] ~]$ grep-o ' [[:d igit:]]\{2,3\} '/etc/passwd
12
10
14
11
12
100
13
30
14
50
99
99
81
81
69
69
499
76
89
89
74
74
500
500
501
27
51
502
502
51
503
503
504
504


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

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/68/wKioL1XjNruCKko4AAEjMibad-0852.jpg "title=" 2.jpg " alt= "Wkiol1xjnruckko4aaejmibad-0852.jpg"/>


4. Add user bash, Testbash, basher, and Nologin user (Nologin user's shell

/sbin/nologin), and then find the line in the/etc/passwd file that has the same user name as its shell name

[[email protected] ~]$ grep ' ^\ (. *\):. *\1$ '/etc/passwd
Sync:x:5:0:sync:/sbin:/bin/sync
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Halt:x:7:0:halt:/sbin:/sbin/halt
Bash:x:505:505::/home/bash:/bin/bash
Nologin:x:508:508::/home/nologin:/sbin/nologin


5. 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] ~]$ grep-e ' ^ (root|centos|user1) '/etc/passwd | Cut-d:-f1,3,7
Root:0:/bin/bash
Centos:509:/bin/bash
User1:510:/bin/bash


6, 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

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/75/wKiom1XkCVGhXXFTAAJKY1LKOLA034.jpg "title=" 4.jpg " alt= "Wkiom1xkcvghxxftaajky1lkola034.jpg"/>

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

[Email protected] ~]$ echo/usr/share/man/man8/iptables-1.4.7.8.gz | Egrep-o ' [^/]+/?$ '
Iptables-1.4.7.8.gz
[Email protected] ~]$ echo/usr/share/man/man8/iptables-1.4.7.8.gz | Egrep-o ' ^/.*/'
/usr/share/man/man8/


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

Ifconfig | grep--color=auto ' [^[:alpha:]|0|[:p unct:]|[:space:]] '

650) this.width=650; "style=" float:left; "src=" Http://s3.51cto.com/wyfs02/M02/72/75/wKioL1XkNyrSrf6eAAKk_ 6a5id8952.jpg "title=" 5.jpg "alt=" Wkiol1xknyrsrf6eaakk_6a5id8952.jpg "/>


Linux OPS Practice-August 27, 2015 Course assignments

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.