Grep basic Regular Expression and egrep extension Regular Expression

Source: Internet
Author: User
Tags egrep

I. Introduction

Grep is used to display the part.) match the rows in string format.

Family: grep egrep fgrp

Ii. How to Use grep

Usage: grep [options] PATTERN file ..

Options:

-I case-insensitive

-- Color Matching highlighted

-O: only show Matching Parts

-N: displays the matched rows and rows.

-V Inversion

-E: use an extended regular expression.

-A n: displays the matched rows and the content of the last N rows.

-B N: displays the matched rows and the content of the N rows before the matched rows.

-C n: display the content of the matched rows and the content of the plus N rows before and after the matched rows.

Basic Regular Expression: REGularEXPression

① Metacharacters:

. Any single character

[] A single character in the specified range

[^] Except for a single character in the specified range

② Number of Characters

* Match any character before

. * Any character of any length

\? Match the first character 0 or 1 time

\ {M, n \} matches the previous character at least m times, at most n times

\ {3 \} matches the previous character three times

\ {1, \} matches the previous character at least once

③ Positioning

^ Beginning of Line

$ End of line

^ $ Blank line

\ <Or \ B any character after it must appear as the first part of the word

\> Or \ B, the character before it must end with the end of the word

\ <Root \> match the word root

Note: You need to add "" when using it.

④ Group

\ (Root \) takes root as a whole

\ (Root \) * root can appear as a whole for any number of times

Group backward reference:

\ (Root \). * \ 1 is equivalent\ (Root \). * \ (root \)

\ 1 reference content in the first one)

\ 2 reference content in second)

....

Extended regular expression:

Use grep-E or egrep

① Metacharacters:

Same as the basic Regular Expression

② Number of Characters

*
?
+ At least once
{M, n} is the same as a basic regular expression, but the escape symbol \

③ Positioning

^
$
\ <
\>

④ Tuples are basically the same as basic regular expressions, but only escape characters are missing)

()
\ 1
\ 2

⑤ Or "|"
Dog | cat dog or cat

Exercise: capture the IP address x. x in ifconfig)

Ideas: x [1-9] | [1-9] [0-9] | [1] [0-9] [0-9] | [2] [0-5] [0-5]

This article is from the keepalive blog, please be sure to keep this source http://jefferyyu.blog.51cto.com/2843635/1300245

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.