The initial knowledge of grep and regular expression

Source: Internet
Author: User
Tags uppercase letter egrep

Most of the time we have a need for text retrieval, like when we use windows, we often search for what we want in a doc or txt document. in the process of using Linux , we also encounter text retrieval scenarios.

in the Linux environment, we use grep,egrep and Fgrepto realize the function of text retrieval. Because Grep,egrep supports the use of regular expressions to represent matching patterns, The text retrieval capabilities of the Linux environment become very powerful. Let's take a detailed description of what grep is and how touse regular expressions with grep to implement text retrieval.

One, what's the regular expression?

It is difficult to describe in a more popular language, copying the contents of various encyclopedia.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5D/BF/wKiom1Uj9KjjQT9RAADTyar7LNI567.jpg "title=" 1.png " Style= "Float:left;" alt= "wkiom1uj9kjjqt9raadtyar7lni567.jpg"/>

as the encyclopedia says, "Regular expressions use a single string to describe a string of syntax rules," where " single string " is the key and difficult point to master regular expressions, and this content is in the third part with grep As an example to describe in detail.

Second, grep and how to use it.

grep [OPTIONS] pattern[file ...]

This is the information given in the Help documentation, where "PATTERN" refers to the " single string " above, whichis more difficult to master and is also grep The key to completing complex search tasks, of course "PATTERN" can also be a simple string. In this section we only use simple strings, highlighting the basic usage of grep and some common "OPTIONS".

Example 1: Search the /etc/passwd file for rows that contain a "root" string.

grep "Root"/etc/passwd

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5D/BF/wKiom1Uj9OSB-7wEAACCLPL8MUM282.jpg "title=" 2.png " alt= "Wkiom1uj9osb-7weaacclpl8mum282.jpg"/>

This is the simplest use of grep,where the line containing the "root" string in the/etc/passwd fileis printed to the screen. In addition,grep has also set some options that make it quicker to search.

--color: For the search to the string to add color, very easy to use a function, but also relatively simple, do not do too many instructions here. In the following example we have defined alias grep= "Grep–color".

-O: Indicates that only matching content is displayed, with the- o option to see the following results :

Grep-o "Root"/etc/passwd

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5D/BF/wKiom1Uj9QKA1MjwAABxy5g8fy8992.jpg "title=" 3.png " alt= "Wkiom1uj9qka1mjwaabxy5g8fy8992.jpg"/>

-I: indicates case insensitive.

Example 2, in the/tmp/test/ directory to write a file containing root,root,root greptest, execute

Grep-i "Root"/tmp/test/greptest

See the following results :

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5D/BF/wKiom1Uj9Rbx3Q7lAACSb21mMOs344.jpg "title=" 4.png " alt= "Wkiom1uj9rbx3q7laacsb21mmos344.jpg"/>

-V: Represents a row that cannot be matched by a pattern


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5D/BA/wKioL1Uj9nSi4cs3AADb1tNNrnw246.jpg "title=" 5.png " alt= "Wkiol1uj9nsi4cs3aadb1tnnrnw246.jpg"/>

Third, use regular expressions with grep to complete the search task.

since grep supports the use of regular expressions to represent "PATTERN", making it incredibly powerful, let's look at some common methods of regular expressions with grep search.

Regular expressions consist of metacharacters and normal characters, in which the meta-characters do not represent the meaning of the characters themselves, but are used to express control or pass-through functions; metacharacters are the key to writing and understanding regular expressions, and here are some common meta-characters.

1, metacharacters for character matching

. : The dot number means matching any single character, such as searching for R followed by two arbitrary characters in the/etc/passwd file , and then the line of T, you can use grep "R." T "/etc/passwd

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5D/BA/wKioL1Uj9oyhXSg2AADFeKl6SbQ177.jpg "title=" 6.png " alt= "Wkiol1uj9oyhxsg2aadfekl6sbq177.jpg"/>

[]: Indicates that any single character within the specified range is matched.

[^] : Represents any single character outside the specified range.

A character set that matches a specified range in a regular expression has the following common expressions:

[0-9] that matches any single number, and [[:d Igit:]] same.

[A-z] represents a match to any single lowercase letter.

[A-z] indicates that any single uppercase letter is matched.

[a-za-z]: means matching any single letter, with [[: Alpha:]].

[[:p UNCT:]] indicates that any punctuation is matched.

[[: Space:]] represents a matching white space character, including spaces and tabs.

[0-9a-za-z] That matches all the letters and numbers, the same [[: Alnum:]] .

Example: In a/etc/rc.d/rc.sysinit file search for n or N, followed by anon-alphabetic line, you can use grep "[Nn][^[:alpha:]]"/ Etc/rc.d/rc.sysinit to say

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5D/BF/wKiom1Uj9V3ik93pAAFQ9uVKlEA677.jpg "title=" 7.png " alt= "Wkiom1uj9v3ik93paafq9uvklea677.jpg"/>

here, give me a mistake I made in my study: searching for a line with no or No in/etc/rc.d/rc.sysinit . The answer to this question should be grep "[nn]o"/etc/rc.d/rc.sysinit rather than grep–i No/etc/rc.d/rc.sysinit,

because the last command matches the three results of no,no,no .

2, metacharacters for the number of times that match

*: Indicates that it matches the preceding character any time.

\? : Indicates matching its left character 0 or 1 times.

\+: Indicates matching its left character 1 or more times.

\{m\}: represents an exact match to its left character M Times.

\{m,n\}: indicates that the number of characters on the left side of the character appears between M and N to be matched by the pattern.

\{m,\}: Indicates that its left character appears at least m times to be searched.

Example:

1) Find out the row of O in/etc/passwd in two consecutive times

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5D/BF/wKiom1Uj9XqiMEZiAAF1Gj6PHIM296.jpg "title=" 8.png " alt= "Wkiom1uj9xqimeziaaf1gj6phim296.jpg"/>

2) Find the line after a in grep.test b appears once or two times.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5D/BF/wKiom1Uj9ZGC4kRJAACMaaxRV8s462.jpg "title=" 9.png " alt= "Wkiom1uj9zgc4krjaacmaaxrv8s462.jpg"/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5D/BA/wKioL1Uj9uzg9ranAABTFx3fnSQ665.jpg "title=" 10.png "alt=" Wkiol1uj9uzg9ranaabtfx3fnsq665.jpg "/>

3, Location-limited.

The use of regular expressions, in addition to the type and number of characters can be expressed, but also to specify the location of the character, the following is described below, the commonly used anchor position representation.

^: Indicates the beginning of the anchor line

$: Indicates the end of the anchor line.

\<: anchor word Head, can also be used \b

\> : Anchor ending, can also be used \b

Example: Find the line in the /etc/passwd file that contains Fedora

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5D/BA/wKioL1Uj9wKT9TTaAAB5jJGA1hM907.jpg "title=" 11.png "alt=" Wkiol1uj9wkt9ttaaab5jjga1hm907.jpg "/>

4, grouping references \ (\)

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, can also be referenced;

\1: Reference, in the pattern from left to right, by the first opening parenthesis and corresponding to the pattern in the closing parenthesis to the content;

\2: reference , from left to right in the pattern, matched by the second opening parenthesis and the pattern in the corresponding closing parenthesis

for example:\ (ab\ (cd\) mn\) \1 \2,\1 represents abcdmn, and \2 represents cd.

Example: Find files in the/etc/passwd file that start with passwd and end with passwd

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5D/BF/wKiom1Uj9c-R6vggAACZvZI6nVE134.jpg "title=" 12.png "alt=" Wkiom1uj9c-r6vggaaczvzi6nve134.jpg "/>


The initial knowledge of grep and regular expression

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.