Regular expressions for Linux systems----grep detailed

Source: Internet
Author: User
Tags echo command processing text egrep


First, the origin of the regular expression:

The "ancestors" of regular expressions can be traced back to early studies of how the human nervous system works. Warren McCulloch and Walter Pitts the two neuroscientists have developed a mathematical approach to describe these neural networks.

In 1956, an American mathematician named Stephen Kleene, on the basis of the early work of McCulloch and Pitts, published a paper titled "Representation of Neural network events", introducing the concept of regular expressions. The regular expression is the expression that describes what he calls "the algebra of the regular set", so the term "regular expression" is used.

Ken Thompson later discovered that this work could be applied to the use of computational search, and Ken Thompson was the main inventor of Unix. The First Utility application of a regular expression is the QED editor in Unix. Since then, regular expressions have been an important part of text-based editors and search tools. Regular expressions with full syntax are used in the format matching of characters and later applied to the field of molten information technology. Since then, regular expressions have evolved over several periods, and the standards are now approved by ISO (International Standards organization) and identified by the Open Group organization.


Ii. what is a regular expression


Baidu Encyclopedia is interpreted as:

Regular expressions, also known as formal representations, regular representations (English: Regular expression, often abbreviated as regex, RegExp, or re in code). A regular expression uses a single string to describe and match a series of strings that conform to a certain syntactic rule.

A regular expression is a logical formula for a string operation, which is a "rule string" that is used to express a filter logic for a string, using predefined specific characters and combinations of these specific characters.


Bird's-brother Linux private dishes interpreted as: The normal expression is the method of processing strings, he is the behavior unit to conduct string processing behavior, regular expressions through some special symbols of the auxiliary, can let users easily reach "search/delete/replace" a particular string of handlers!


Popular explanation: Regular expressions are a way to get specific content in text by matching certain rules.


Classification of regular Expressions:

1. Basic Regular Expressions (basic Regular expression also known as basic RegEx abbreviation BREs)

2. Extended Regular Expressions (Extended Regular expression also known as Extended RegEx abbreviation EREs)


Noun Explanation: The character is the computer software processing text when the most basic unit, may be letters, numbers, punctuation, spaces, line breaks, Chinese characters and so on. A string is a sequence of 0 or more characters. Text is the literal, string. To say that a string matches a regular expression, usually refers to a part (or parts of it) in the string that satisfies the condition given by an expression.


Third, why to use regular expressions


When we are dealing with a very large number of files in the case of limited time, or when we are dealing with very big text files, the proper use of regular expressions allows us to quickly retrieve the file or text information we want, and can print the matching file or text information, which can save a lot of time.

In addition, regular expressions are important to system administrators! Because the system does not fill out will produce a lot of information, some of the important information is only to inform, at this time, the administrator can through the formal expression of the function to print out important information, and produce easy to consult the report to simplify the management process.


Third, how to correctly use regular expressions

The main program for Linux to process regular expressions is grep, which is explained in detail so that the reader can understand the definition of regular expressions a step further and be proficient in using grep text Search tools in Linux systems.


3.1. What is grep:

grep (Global search Regular expression (RE) and print out of the line, full search of regular expressions and print out the lines) is a powerful text search tool, It can search for text using regular expressions (regular expression metacharacters and normal character combinations) and prints the matching lines.


The grep family of Unix includes grep, Egrep, and Fgrep. Egrep and Fgrep commands are only a small difference from grep. Egrep is the extension of grep, which supports more re metacharacters, and fgrep is fixed grep or fast grep, which regards all the letters as words, that is, the metacharacters in the regular expression represents the literal meaning back to itself, no longer special. Linux uses the GNU version of grep. It is more powerful and can use the Egrep and FGREP functions with the-G,-e,-f command line options.


3.2, the use of grep:

grep [OPTIONS] PATTERN [FILE ...]

grep [OPTIONS] [-E PATTERN |-f file] [FILE ...]

      

     options main parameters are as follows:         -o:  only-matching  shows only what is matched to the pattern          -i: ignore case    character case insensitive          -v: invert-match  Display rows that cannot be matched to a pattern          -n: number  output matching rows, plus the number of rows in the file before          -c: count   Output Matching number (number of lines)           -e: extended-regexp using extended Regular Expressions  POSIX          -a #:after, displays data after matching string  #  rows           -b #:before, show data before matching string  #  rows           -C #  Show data          --before and after matching string  #  rows color=auto:  highlight the matching color in a specific color   The        --color= parameter is as follows:              auto              Always             never

eg

(1) To display the matching keywords in a specific color:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5C/24/wKioL1Ubq3qRiN62AAD-CZ1FDoQ724.jpg "title=" Color.jpg "alt=" Wkiol1ubq3qrin62aad-cz1fdoq724.jpg "/>

(2), match the line containing the root keyword in the/etc/passwd directory

[[email protected] ~]# grep "root"/etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/ Nologi[[email protected] ~]#

(3), the/etc/passwd file, there is the root of the row is taken out, while displaying the line number of these lines in the/etc/passwd file

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5C/2B/wKiom1UbtjiSmPyfAACUhts4by4306.jpg "title="-n.jpg "alt=" Wkiom1ubtjismpyfaacuhts4by4306.jpg "/>

(4), displays the lines in the/proc/meminfo file that begin with uppercase or lowercase s, in two ways:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5C/2C/wKiom1UbxN3gS38zAAEiZ4Y6V8w864.jpg "title=" I.jpg " alt= "Wkiom1ubxn3gs38zaaeiz4y6v8w864.jpg"/>

(5), display the/etc/passwd file whose default shell is non-/sbin/nologin user;

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5C/2C/wKiom1UbxXXh1e_BAACSrQHvVTk642.jpg "title=" V.jpg " alt= "Wkiom1ubxxxh1e_baacsrqhvvtk642.jpg"/>


3.3. Basic regular expression meta-characters:

Character match:        . :  matches any single character          []:  matches any single character in the specified range         [^] :  Matches any single character outside the specified range         [0-9],[[:d igit:]]        all numbers;         [^0-9],[^[:d igit:]]     Any character except the number         [a-z],[[:lower:]]        All lowercase letters         [A-Z], [[:upper:]]       All capital letters         [[:space:]]              all whitespace characters         [[:p unct:]]              All Punctuation          [0-9a-za-z],[[:alnum:]] all the lettersAnd the numbers;        [a-za-z],[[:alpha:]]     all the letters 
*: Matches the left character any time, indicating 0 times, 1 times or more; *: arbitrary length of any character working with greedy mode \?:0 or 1 times; indicates that its left character is optional \+: 1 or more times; matches its left CI character appears at least 1 times \{m\}: M times: Indicates that its left character appears precisely m Times \{m,n\}: At least m times, up to n Times \{0,n\}: Up to N Times \{m,\}: at least m times

Eg: Create a grep.txt file with the following content:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5C/31/wKiom1Ub2ELy9C8jAAA68vFwCHA512.jpg "title=" Greo Txt.jpg "alt=" Wkiom1ub2ely9c8jaaa68vfwcha512.jpg "/>

. Match any single character

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5C/31/wKiom1Ub2L3SsL0TAAAyV6y_pBA559.jpg "title=" Q.jpg " alt= "Wkiom1ub2l3ssl0taaayv6y_pba559.jpg"/>

Use. * matches any length character

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5C/32/wKiom1Ub2V-zNMJ9AABFAYs_4SY084.jpg "title=" 22.jpg "alt=" Wkiom1ub2v-znmj9aabfays_4sy084.jpg "/>

Matches a line with at least 3 B characters in front of C

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5C/32/wKiom1Ub2mninOq1AAAx5ITmoPI526.jpg "title=" 55.jpg "alt=" Wkiom1ub2mninoq1aaax5itmopi526.jpg "/>



(1), find one or two digits in the/etc/passwd file;

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5C/26/wKioL1Ubx6vw42UQAAGxjsQYhbc319.jpg "title=" 0.jpg " alt= "Wkiol1ubx6vw42uqaagxjsqyhbc319.jpg"/>

(2), find the/etc/passwd file contains o consecutive two times the row

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5C/2C/wKiom1UbygqDJwZeAAD1oDGPW_A545.jpg "title=" oo.jpg "alt=" Wkiom1ubygqdjwzeaad1odgpw_a545.jpg "/>



Position anchoring: Used to search for the PATTERN in the line where you want to search ^: Anchor beginning ^tattern//search for lines beginning with PATTERN $: Anchor Line end pattern$// Search for lines ending in PATTERN ^pattern$//Search matches entire line contents # grep "^[[:space:]]*$"/etc/rc.d/rc.sysinit ^$: matches blank lines;

eg

(1), search the/etc/passwd file in the beginning of the root line

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5C/26/wKioL1UbvvrQI0WDAABxeHDtj9s312.jpg "title=" r1.jpg "alt=" Wkiol1ubvvrqi0wdaabxehdtj9s312.jpg "/>

(2), search the/etc/passwd file with bash as the line of the end of the line

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5C/2B/wKiom1Ubvs-jRG86AAEXVMyQf6M551.jpg "title=" Ciw7.jpg "alt=" Wkiom1ubvs-jrg86aaexvmyqf6m551.jpg "/>

(3), search for lines with whitespace characters in the/etc/rc.d/rc.sysinit file, and display the line number of each line in the file

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5C/2B/wKiom1UbwQfzZOW3AABaScqJiVs009.jpg "title=" h.jpg " alt= "Wkiom1ubwqfzzow3aabascqjivs009.jpg"/>



Word anchoring: A continuous string consisting of non-special characters \<: Anchor Word head, also available \b \<pattern, or \bpattern \>: Anchor ending, also available \b Pattern\>, or Pat tern\b \<pattern\>: matches the entire word that PATTERN can match

eg

(1), search/etc/passwd file with root as the first line of the word

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5C/2B/wKiom1Ubusbim1HIAAETWESAw38315.jpg "title=" B.jpg " alt= "Wkiom1ubusbim1hiaaetwesaw38315.jpg"/>

(2), search/etc/passwd file in the Nologin Word as the ending line

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5C/2B/wKiom1Ubu-GgnwB5AAIjYFRUD8s052.jpg "title=" W.jpg " alt= "Wkiom1ubu-ggnwb5aaijyfrud8s052.jpg"/>

(3), search the/etc/passwd file contains the word shutdown line

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5C/26/wKioL1UbvgTQxS2JAAD_zo0o5iw397.jpg "title=" ci.jpg "alt=" Wkiol1ubvgtqxs2jaad_zo0o5iw397.jpg "/>



11

Group: \ (\) Note: The patterns in the grouping, the characters that match in a particular match, can be memorized by grep (stored in the built-in variables, which are \1,\2,... ), therefore, the matching result can also be referred to \ 1: Reference, the pattern from left to right, by the first opening parenthesis and the corresponding right parenthesis in the pattern to match to the content; \ 2: Reference, in the pattern from left to right, by the second opening parenthesis and corresponding to the pattern in the right parenthesis match to the content;

eg

(1), create the Fenzu.txt file its contents as follows, find the second word and the fourth Word is a line of words

He like his lover.

He like his liker.

She Love her lover.

She Love her liker.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5C/2C/wKiom1Ubw7igoHTIAACILeHhmaU105.jpg "title=" Fenzu.jpg "alt=" Wkiom1ubw7igohtiaacilehhmau105.jpg "/>



Cases:

1. Use the echo command to output an absolute path, using grep to remove its base name;

[Email protected] ~]# echo/etc/passwd/| Grep-o "[[: Alnum:]]\+\/\?$" Passwd/[[email protected] ~]# echo/etc/passwd | Grep-o "[[: Alnum:]]\+\/\?$" Passwd[[email protected] ~]#

2, find out the results of the ifconfig command between 1-255 numbers;

[[email protected] ~]# ifconfig | grep -e  "\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \> "eth0      link encap:ethernet   HWaddr 00:0C:29:49:03:2B             inet addr:192.168.67.129  Bcast:192.168.67.255  Mask:255.255.255.0           inet6 addr: fe80::20c:29ff:fe49:32b/64 scope: Link          up broadcast running multicast   MTU:1500  Metric:1          RX  bytes:860103  (839.9 kib)   TX bytes:510502  (498.5 kib)            inet addr:127.0.0.1  Mask:255.0.0.0           Inet6 addr: ::1/128 scope:host          up  loopback running  mtu:65536  metric:1

3, write a mode, can match the reasonable IP address;

1.0.0.1--239.255.255.255

Grep-e "\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-3][0-9]) \>\.\< ([0-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \>\.\< ([0-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \>\.\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \> "




This article is from the "impermanence" blog, please be sure to keep this source http://1inux.blog.51cto.com/10037358/1627442

Regular expressions for Linux systems----grep detailed

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.