Describes the regular expressions and the usage and features of the text processing tool grep and egrep.

Source: Internet
Author: User
Tags expression engine egrep

1. grep

(1) grep Introduction

Grep: global search Regular Expression and print out the line. Global Search for regular expressions and output suitable rows.

Grep is a powerful text search tool that performs row-by-row search on the target file based on the user-specified text mode (search criteria) to display matching rows. (Search for text only)

The grep family of UNIX contains grep, egrep, and fgrep.

(2) grep usage

Syntax format: grep [Option]... 'pattern' file...

Option text mode file

Command Options:

-- Color = Auto: indicates the color of the matched string. The color is red by default)

-V: reverse selection (only the rows that are not matched are displayed)

-O: only display the matched string, not the row of the string.

-I: (ignore-case) case-insensitive characters

-A #: displays the matched row and the row # The row (# indicates a number)

-B #: Display matched rows and the beginning of the row # Rows

-C #: displays the matched rows, the row's front # The row and the row's back # The row

-E: supports the use of extended regular expressions (equivalent to the use of the egrep command)

Grep does not match the entire line, but matches by string. (When a specified string is matched for the first time in a row, it does not jump directly to the next row to match, but continues matching in the row .)

Grep works in greedy mode: matching as much as possible

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-Right: 0px; "Title =" wps_clip_image-23823 "border =" 0 "alt =" wps_clip_image-23823 "src =" http://img1.51cto.com/attachment/201407/13/5970897_14052611146cFC.png "Height =" 246 "/>

To use the advanced usage of grep, you must combine the regular expression with the pipeline. Therefore, let's open the fog and walk into the garden of regular expressions.

II. Introduction to Regular Expressions

Regular Expression:

Regular Expression, also known as regular expression and regular expression (English: Regular Expression, often abbreviated as RegEx, Regexp or Re in code), is a concept of computer science.

Regular Expressions use a single string to describe and match a series of strings that conform to a certain syntax rule. In many text editors, regular expressions are usually used to retrieve and replace texts that match a certain pattern.

In fact, regular expressions are a type of character writing mode. Many of these characters do not represent their literal meanings, but express functions such as control or wildcard;

Metacharacters:

Special characters with special meanings in regular expressions (similar to keywords in C );

It does not represent its literal meaning, but is used for additional functional descriptions.

Category:

Regular Expressions are divided into two types: "basic regular expressions" and "extended regular expressions"

Grep has the built-in "basic regular expression" function. You can also use grep-e to use the "extended regular expression" function.

Egrep has built-in "extended regular expression" function.

Fgrep: regular expressions are not supported.

III. Basic Regular Expressions

Metacharacters of the basic regular expression:

(1) character matching:

.: Match any single character

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-Right: 0px; "Title =" wps_clip_image-29481 "border =" 0 "alt =" wps_clip_image-29481 "src =" http://img1.51cto.com/attachment/201407/13/5970897_140526112956gw.png "Height =" 202 "/>

[]: Match any single character in the specified range

[0-9], [[: digit:] (a single 0-9 number)

[A-Z], [[: lower:] (a single lowercase letter)

[A-Z], [[: Upper:] (single capital letter)

[[: Space:] (Space)

[[: Punct:] (single punctuation)

[[: Alpha:] (a single letter, including uppercase and lowercase)

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-Right: 0px; "Title =" wps_clip_image-29639 "border =" 0 "alt =" wps_clip_image-29639 "src =" http://img1.51cto.com/attachment/201407/13/5970897_1405261143R1WM.png "Height =" 110 "/>

[[: Alnum:] (single digit or letter)

[^]: Match any single character inside or outside the specified range

(2) times matching metacharacters: used to specify the number of times that a character before it can appear

*: Any length. The first character can appear at any time.

\? : 0 or 1 time. The first character is dispensable.

\ {M \}: m times. The first character must appear m times.

\ {M, N \}: At least m times, at most N times (M <n)

\ {M, \}: At least m times

\ {0, n \}: up to n times

. *: Any character of any length

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-Right: 0px; "Title =" wps_clip_image-24154 "border =" 0 "alt =" wps_clip_image-24154 "src =" http://img1.51cto.com/attachment/201407/13/5970897_1405261164sGsm.png "Height =" 232 "/>

(3) positioning:

^: The beginning of the line is anchored; written on the leftmost part of the Mode

$: Anchor at the end of the line: the rightmost side of the write mode

^ $: Blank line

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-Right: 0px; "Title =" wps_clip_image-31177 "border =" 0 "alt =" wps_clip_image-31177 "src =" http://img1.51cto.com/attachment/201407/13/5970897_1405261166rmnB.png "Height =" 61 "/> (look for blank lines, only available)

\ <: Specifies the beginning of a word, which appears on the left side of the word. \ B is also available.

\>: Specifies the end of a word, which appears on the right of the word. \ B is also available.

(A string consisting of continuous characters that do not contain special characters is called a word)

(4) group:

\ (\): Group format

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-Right: 0px; "Title =" wps_clip_image-29876 "border =" 0 "alt =" wps_clip_image-29876 "src =" http://img1.51cto.com/attachment/201407/13/5970897_14052611817VgP.png "Height =" 142 "/>

The pattern matching content in the group can be stored in the memory by the Regular Expression Engine and can be referenced later.

Reference Number: The left parentheses (from left to right) and matching brackets (no matter how many parentheses are embedded .)

For example, \ (A \ (B \ (C \) Mn \ (x \)\). * \ 1 (\ 1 references the first left brace and the matching content between the matching right brace)

#: Reference the content matched with the nth parenthesis, instead of the pattern itself (# indicates a number) (for example, \ (AB \ (x \). * \ 2: \ 2 references the Matching content of \ (x)

For example:

\ (AB \? C \). * \ 1

Abcmnaaa abcmnabc (matched successfully) abcmnac acxyac (matched successfully)

Iv. Extended Regular Expressions

Egrep has the built-in "extended regular expression" function. You can also use grep-e to use the "extended regular expression" function.

Grep-e 'pattern' file...

Egrep 'pattern' file...

(1) character matching:

.: Match a single arbitrary character

[]: Match any single character in the specified range

[^]: Match any single character out of the specified range

(2) times matching:

*: Any time

? : 0 or 1 time (optional)

+: At least 1 time; (equivalent to \ {1, \} of the basic regular expression ,\})

{M}: exact match m times

{M, n}: At least m times, at most N times (M <n)

{M ,}: at least m times

{0, n}: up to n times

(3) positioning:

^: First line anchored

$: Anchor at the end of a row

\ <, \ B: the beginning of the word.

\>, \ B: ending point

^ $, ^ [[: Space:] * $: blank line

(4) group:

():

Reference: \ 1, \ 2, \ 3 can also be used

|: Or (extended regular expression exclusive)

For example, a | B indicates A or B.

Conc | Cat: conc or cat

Con (c | C) Cat: Concat or Concat

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-Right: 0px; "Title =" wps_clip_image-16443 "border =" 0 "alt =" wps_clip_image-16443 "src =" http://img1.51cto.com/attachment/201407/13/5970897_1405261210YSyE.png "Height =" 398 "/>

This article from the "hengzeng" blog, please be sure to keep this source http://hjqjk.blog.51cto.com/5970897/1437663

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.