Basic usage of regular expressions and grep 9

Source: Internet
Author: User

1. grep: Global RE Printing

Regular Expression


Text filtering tool, which can perform row-by-row search based on the specified pattern)


The content of the file and the matched lines are displayed;


Mode: it is a matching condition consisting of metacharacters of regular expressions and other characters;


RE: Regular Expression

Base RE

Extended RE


Grep [option]... 'pattern' FILE...

-V reverse value;

-- Color = auto-matched characters are displayed in red and bold;


1. Basic Regular Expression metacharacters


.: Match any single character;

For example, grep 'C. U'/proc/cpuinfo;


..: Match 2 characters;


[]: Any single character in the specified range;

For example, grep -- color = auto'c .. [a-z] '/proc/cpuinfo;


[^]: Any character out of the specified range;


2. Times matching greedy match)


*: Match the first character 0, 1 or multiple times;

For example, AB * c: abc, abbc, and ac do not match.) abdc (indicating the appearance of B


Times );


Job (grep.txt file, one of which has the tab key ):

Find AB, a B, accccb;

Grep 'a [[: space:] * B '/tmp/grep.txt;


? : Match the first character 0 or 1 time;


\ {M, n \}: Where \ is conversion, {} is to avoid shell expansion, at least m


N times at most;

\ {M, \}: At least m times;

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

\ {M \}: m times;


Job:

Exercise the following match:

A [a-z] \ {0, 2 \} c: abc, aaaac, accccc;


3. Anchor

R.. t: root, chroot, rooter;

This is root.

There is chroot.


Word anchoring:

\ <: The beginning of the anchor; for example, \ <r. t;

\>: Specifies the end of a word, for example, root \>;


First line anchored:

^ Escape Character): for example, ^ root;


Anchor at the end of the line:

$: For example, root $;


Job:

A. How to display the rows whose end is root and can contain a punctuation mark;

Grep 'root [[: punct:]? $ 'File = grep' root [[: punct:] \ {0, 1 \} $ 'file;

B. How to display blank rows starting with "root;

Grep '^ [[: space:] * root 'file;


. *: Match any character of any length;

For example, a. * B, a and B can contain any character of any length;


4. Group

Xabababy and AB appear in groups;


\ (\), X \ (AB \) * y


Forward reference:

He love his lover.

She like her liker.

He love his liker.

She like her lover.


L. e. * l.. er;

\ (L. e \). * \ 1r is followed by a number 1 );



This article is from the blog "Nick Liu's blog", please be sure to keep this source http://zkhylt.blog.51cto.com/3638719/1301589

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.