grep, Egrep, and corresponding regular expressions and usages

Source: Internet
Author: User
Tags expression engine egrep

1.grep and Egrep Introduction

grep full name is the global Regular Expressiongrep is a powerful text search tool for searching and filtering specific characters. Support regular expressions, can help us to filter, filter the common work. and can be used with a variety of commands through the pipeline, flexible and convenient. Its use rights are for all users.

1.1 regular Expressions (RegularExp ression REGEX )

Before learning the grep tool, we should learn the regular expression, in general, as long as the regular expression to learn, you can play the role of grep and other tools.

A regular expression (Regular expression, REGEX) is a pattern written by a class of special characters and text characters, some of which do not represent their literal meaning, but are used to denote control or the function of a wildcard. In simple terms, a specific string that conforms to some grammatical rule semantics. It's just a way of thinking an expression is not a tool. Regular expressions can help us implement common functions such as filtering and filtering. As long as the tools we use support this expression, the tool can handle strings of regular expressions.

1.1.1 classification of regular Expressions

Regular expressions are divided into basic regular expressions and extended regular expressions. They can be roughly divided into character matches, number of times matching positions anchored and grouped and referenced. Here's a look at their metacharacters presentation methods.

A. Character reference

. Match any single character
[  ] Matches any single character in the range
[^ ] Any of the matches outside the range

Example 1: A line that starts with R in the file/etc/passwd file and ends with an arbitrary character in the middle of T.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/22/wKiom1bg3laBcrd2AAAbRK4w-6w178.png "title=" R. . T.png "alt=" Wkiom1bg3labcrd2aaabrk4w-6w178.png "/>

B. Number of Matches

Use the number of occurrences of the character that you want to specify the number of occurrences, and the default work in greedy mode.

1 TD width= "280" valign= "Middle" align= "center" >
Basic Regular Expressions
Extending regular Expressions

*

Any of the preceding characters any time

* Any of the preceding characters any time

\+

match the preceding character at least 1

+

\?

Match the previous 0 times or 1 times, that is, the preceding characters are optional

Match the preceding 0 1 times, that is, the preceding character is optional

\{m\}

The characters in front of it appear m m

{m} The characters in front of it appear m m is a non-negative integer

\{0,n\}

At least m times

{m,0} m times

\{m,\}

Up to n times

{0,n} Maximum N Times

\{m,n\}

Its characters appear M times,m is a nonnegative integer;[m,n] front

{M,n} its characters appear m Times, m is a non-negative integer; [M,n] Front

Example: Displays rows with R and R followed by 1 or 2 o in the/etc/passwd file.

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/7D/27/wKioL1bhFg6QlI7zAAAg-gdOB3A759.png "title=" image 66. PNG "alt=" Wkiol1bhfg6qli7zaaag-gdob3a759.png "/> c. Position anchoring

Restricts the use of pattern search text, which restricts the text that the pattern matches to where it appears only in the destination text.

Basic Regular Expressions
Extending regular Expressions
^

Anchor at the beginning of the line; for the leftmost of the pattern,^pattern

^ anchor at the beginning of the line, to the left of the pattern, ^pattern
$

End of line anchoring; for the right side of the pattern,pattern$

$ end of line anchoring; for the right side of the pattern, pattern$
\< or \b

The first anchor of the word, used for the left side of the word pattern, is \<pattern, \bpattern

\< or \b end of line anchoring; for the right side of the pattern, pattern$
\<pattern\>

Word anchoring

\<pattern\> Word anchoring
>\ or \b

Suffix anchor for the right side of the word pattern, formatted as pattern\> pattern\b

>\ or \b The ending anchor is used for the right side of the word pattern, in the form pattern\>, pattern\b
Example: Find the row containing root in the/etc/passwd file.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/29/wKiom1bhFiODKNUyAAAXDJK4sAM369.png "title=" 88.png "alt=" Wkiom1bhfiodknuyaaaxdjk4sam369.png "/>

D. grouping and referencing.

Sometimes we need to combine a whole and then make a match based on the combined character, which needs to be grouped and referenced.

1. Grouping

(pattern) treats the character that this PATTERN matches to as an indivisible whole;

2 references

The characters in the block brackets that match the pattern are automatically recorded in the internal variables by the regular expression engine, which are \1, \2, \3, ... \ n

\ n: The string that matches the pattern between the nth opening parenthesis in the pattern and the closing parenthesis that matches it, (not the pattern, but the result of the pattern match), so that we can refer to the overall back reference.

Example: pat1\ (pat2\) pat3\ (pat4\ (pat5\) pat6\)

where \1 is (PAT2), \2 is (PAT4\PAT5) ... \ n = (pat2\) pat3\ (pat4\ (pat5\) pat6\)


In addition, Egrep also supports a "|" As a group separator, which is different from grep.

Example: In order to better understand the group and the back reference, we create a file test ourselves.

#vi Test Enter the following to save the exit.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/27/wKiom1bhDkHyuMhVAAASOGpZ9Z0397.png "title=" 3333. PNG "alt=" Wkiom1bhdkhyumhvaaasogpz9z0397.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7D/2A/wKiom1bhHpDgk9lWAAAe7QFbV7M246.png "title=" 13.png "alt=" Wkiom1bhhpdgk9lwaaae7qfbv7m246.png "/>

look for the row output containing (A.D) under the test file, where "." Represents any character. Referring to the previously matched characters, we will find that there is a missing line because the "." In the reference. is no longer a person with a character, but a definite good character.


1.2 grep Other common options and usage examples.

--color=auto: Highlighting the matched text after coloring, because CENTOS7 defines the alias, so here we use "/" to execute the command itself.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/27/wKioL1bhFsCgFfF8AAAbgW-xsLw549.png "title="-- Color.png "alt=" Wkiol1bhfscgfff8aaabgw-xslw549.png "/>

-I: ignore character case;

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7D/29/wKiom1bhFy7xJanlAAAdH0M5tuM942.png "title="-i.png "alt=" Wkiom1bhfy7xjanlaaadh0m5tum942.png "/>

-O: Displays only the text that matches to itself;

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7D/29/wKiom1bhF46BZB1pAAARref4JMs925.png "title=" itself. png "alt=" Wkiom1bhf46bzb1paaarref4jms925.png "/>

-V,--invert-match: reverse match;

-Q,--quiet,--silient: Silent mode, do not output any information;

-E,--extended-regexp: supports the use of extended regular expressions

-F,--fixed-strings: Supports the use of fixed strings, does not support regular expressions, and is equivalent to Fgrep;

-G,--basic-regexp: Supports the use of basic regular expressions;

-P,--perl-regexp: supports the use of pcre regular expressions;

-E pattern,--regexp=pattern: multi-mode mechanism;

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M01/7D/29/wKiom1bhGF_wT3TZAAAn0wqgOTw485.png "title=" image 1011.png "alt=" Wkiom1bhgf_wt3tzaaan0wqgotw485.png "/>

-F file,--file=file:file a text file containing a pattern for each line, the grep script;

-A num that displays the matching lines as well as the post num lines.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7D/27/wKioL1bhGb_SccwGAAAjKkZ1B3E296.png "title=" 11.png "alt=" Wkiol1bhgb_sccwgaaajkkz1b3e296.png "/>

-B num, showing the row to match and the previous NUM line.

-C num, which displays the matched rows and the front and back NUM rows.


650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/27/wKioL1bhGhfgfvn8AAAjPmSWoRE878.png "title=" 12.png "alt=" Wkiol1bhghfgfvn8aaajpmswore878.png "/>


This article is from the "11243407" blog, please be sure to keep this source http://11253407.blog.51cto.com/11243407/1749597

grep, Egrep, and corresponding regular expressions and usages

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.