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://www.178linux.com/ueditor/php/upload/image/20150518/1431959526140463.jpg "title= "1431959526140463.jpg" alt= "1.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://www.178linux.com/ueditor/php/upload/image/20150518/1431959858169135.jpg "title= "1431959858169135.jpg" alt= "2.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://www.178linux.com/ueditor/php/upload/image/20150518/1431960190164226.jpg "title= "1431960190164226.jpg" alt= "3.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://www.178linux.com/ueditor/php/upload/image/20150518/1431960515911997.jpg "title= "1431960515911997.jpg" alt= "4.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://www.178linux.com/ueditor/php/upload/image/20150518/1431962706890926.jpg "title= "1431962706890926.jpg" alt= "5.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://www.178linux.com/ueditor/php/upload/image/20150518/1431960981386967.jpg "title= "1431960981386967.jpg" alt= "6.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://www.178linux.com/ueditor/php/upload/image/20150518/1431961263115812.jpg "title= "1431961263115812.jpg" alt= "7.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://www.178linux.com/ueditor/php/upload/image/20150518/1431962021113971.jpg "title= "1431962021113971.jpg" alt= "8.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://www.178linux.com/ueditor/php/upload/image/20150518/1431962937111020.jpg "title= "1431962937111020.jpg" alt= "9.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://www.178linux.com/ueditor/php/upload/image/20150518/1431963348651501.jpg "title= "1431963348651501.jpg" alt= "10.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://www.178linux.com/ueditor/php/upload/image/20150519/1431965057125437.jpg "title= "1431965057125437.jpg" alt= "11.jpg"/>


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

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.