The text-processing tool grep and the initial knowledge regular expression of Linux

Source: Internet
Author: User
Tags expression engine

A), the three major text processing of Linux grep:

grep (Global search REgular expression and print out of the line, fully search for regular expressions and print them out): After giving a list of files or standard output, grep searches for text that matches one or more regular expressions , and only the lines or text that match (or do not match) are output. ----Excerpt from Wikimedia encyclopedia

The grep family includes:

Grep:

Egrep: grep that supports extended regular expressions, equivalent to GREP-E.

fgrep:fixed grep or fast grep, regular expressions are not supported.


b), syntax format:

grep [option] ... ' PATTERN ' FILE ...

Note: the string is searched by line.

Common Command options:

-V: Reverses the selection, which shows the rows that do not match.

-O: Displays only the matched string, not the line where the string is located.

-i:ignore-case, ignoring the character case.

-E: Supports the use of extended regular expressions.

-A #: Displays the matching line and the next # line.

-B #: Displays matching lines and upper # lines.

-C #: Displays matching lines and up and down # lines.

--color=auto: Match to the string display color.



c), Regular expression:

Regular Expression, often abbreviated in code as a regex, regexp, or RE, is a concept of computer science. A regular expression uses a single string to describe and match a series of strings that conform to a certain syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that conforms to a pattern. ----Excerpt from Wikimedia encyclopedia


Four), meta-characters:


1), Character matching:

.: Matches any single character

[]: Matches a character within a specified range

[^]: matches a character that is not within the specified range

[:d igit:]: numeric character, i.e. [0-9]

[: Lower:]: lowercase characters, i.e. [A-z]

[: Upper:]: Uppercase characters, i.e. [A-z]

[: Alpha:]: literal character, i.e. [a-z,a-z]


[: Alnum:]: literal numeric character, i.e. [a-z,a-z,0-9]

[: Xdigit:]: hexadecimal number, i.e. [0-9,a-f,a-f]

[:p UNCT:]: Punctuation

[: Space:]: white space character

[: Graph:]: non-whitespace characters

[: Cntrl:]: Control character

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


2), the number of times matching:

*: 0 or more previous characters.

. *: Any character at any time

\?: 0 or one-time previous character.

\{m\}: The previous character repeats M times.

\{m,n\}: The previous character M to n times.

X\{m,\}: The previous character is at least m times.

X\{0,n\}: The previous character is up to n times.


3), Position anchoring:


^: Anchor at the beginning of the line.

$: End of line anchoring.

^$: Blank line.

\<,/b: Anchors the first word, appearing on the left side of the word.

\>,\b: Anchors the ending, appearing on the right side of the word.

\<, .... \>,\b. \b: The word lock character.


4), grouping and referencing:

\(.. \):

The patterns in the grouping match to the content that can be remembered in memory by the regular expression engine and then referenced.

\#:

Refers to the content that the nth parenthesis matches to, not the schema itself.

V), extended regular expression

1), Character matching:

Is the same as the basic regular expression.

2), number of times matching

Is the same as the basic regular expression and does not need to be translated.

+: One or more previous characters.

A|B: Branch, A or B,

Con (c|c) at:concat or concat

Conc|cat:conc or Cat

3), Position anchoring:

Is the same as the basic regular expression.

4), grouping and referencing

As with the basic regular expression, the grouping does not need to be translated.




VI), example description:

01. Displays lines in the/proc/meminfo file that begin with uppercase or lowercase s.

#grep--color=auto-i ' ^s '/proc/meminfo#grep--color=auto-e ' ^ (s| S) '/proc/meminfo

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6D/02/wKioL1VaIS6wl2oBAACkJsrR-YA080.jpg "title=" 1.jpg " alt= "Wkiol1vais6wl2obaackjsrr-ya080.jpg"/>


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

#grep--color=auto-v "/sbin/nologin$"/etc/passwd | Cut-d:-f1

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6D/07/wKiom1VaH8DgZ6WFAACEa2GwJXs040.jpg "title=" 2.jpg " alt= "Wkiom1vah8dgz6wfaacea2gwjxs040.jpg"/>


03. Display the user whose default shell is/bin/bash in/etc/passwd file;

Further: Displays only the user whose ID number is the highest in the above results.

#grep--color=auto "/bin/bash$"/etc/passwd | SORT-T:-k3-n | Tail-n 1 | Cut-d:-f1,3,7

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6D/07/wKiom1VaH87zvByiAABX6F_5fcU970.jpg "title=" 3.jpg " alt= "Wkiom1vah87zvbyiaabx6f_5fcu970.jpg"/>


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

#grep--color=auto "\<[[:d igit:]]\{1,2\}\>"/etc/passwd

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6D/07/wKiom1VaH_SAur4EAAKhKTsQrTw728.jpg "title=" 4.jpg " alt= "Wkiom1vah_saur4eaakhktsqrtw728.jpg"/>


05. Displays the lines in/boot/grub/grub.conf that begin with at least one whitespace character.


#grep--color=auto "^[[:space:]]\{1,}"/boot/grub/grub.conf

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6D/02/wKioL1VaIXzAkX27AAElBYOd6Tg786.jpg "title=" 5.jpg " alt= "Wkiol1vaixzakx27aaelbyod6tg786.jpg"/>


06. Display the/etc/rc.d/rc.sysinit file, starting with #, followed by at least one white-space character, and then with at least one non-whitespace character line.

#grep--color=auto "^#[[:space:]]\{1,\}[^[:space:]]\{1,\}"/etc/rc.d/rc.sysinit

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6D/07/wKiom1VaIBGTNXIbAASQtcoFdI0282.jpg "title=" 6.jpg " alt= "Wkiom1vaibgtnxibaasqtcofdi0282.jpg"/>


07. Find the line that ends with ' LISTEN ' in the Netstat-tan command execution result.

#netstat-tan | grep--color=auto "listen[[:space:]]*$"

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6D/07/wKiom1VaIBuDj9fjAADPi-vgbHc449.jpg "title=" 7.jpg " alt= "Wkiom1vaibudj9fjaadpi-vgbhc449.jpg"/>


08, add User Bash, Testbash, basher, Nologin (shell for/sbin/nologin), and find the current system on its user name and the default shell of the same user.

#grep--color=auto "^\ ([[: Alpha:]]\{1,\}\):. */\1$"/etc/passwd

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6D/02/wKioL1VaIZ6AGznBAADlRRlcy_8654.jpg "title=" 8.jpg " alt= "Wkiol1vaiz6agznbaadlrrlcy_8654.jpg"/>


09. Displays the default shell for root, Rhel, or CentOS users on the current system.

#grep--color=auto-e "^ (Root|rhel|centos):. *"/ETC/PASSWD | Cut-d:-f1,7

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6D/02/wKioL1VaIbjyaAYMAAB-ygYdW7g081.jpg "title=" 9.jpg " alt= "Wkiol1vaibjyaaymaab-ygydw7g081.jpg"/>


10. Find a word followed by a set of parentheses "()" line in the/etc/rc.d/init.d/functions file

#grep--color=auto-e "\<[[:alpha:]]+\>\ (\)"/etc/rc.d/init.d/functions

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6D/07/wKiom1VaIEmCEbL4AADVTW_Jxp0155.jpg "title=" 10.jpg "alt=" Wkiom1vaiemcebl4aadvtw_jxp0155.jpg "/>


11. Find the number between 1-255 in the result of ifconfig command;

#ifconfig | grep--color=auto-e "\< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> "

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6D/07/wKiom1VaIFOSpn54AAGiK_c0s-M957.jpg "title=" 11.jpg "alt=" Wkiom1vaifospn54aagik_c0s-m957.jpg "/>


This article is from the "Pony Learning Record" blog, make sure to keep this source http://masachencer.blog.51cto.com/8683770/1652635

The text-processing tool grep and the initial knowledge regular expression of Linux

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.