Regular expressions for Linux grep, Egrep

Source: Internet
Author: User
Tags uppercase letter egrep

Read Catalogue

    • Simple Introduction
    • Some options and functions of regular expressions
    • Basic Regular Expressions
    • Extending regular Expressions

In the transport Wei Chong expression for the author must learn and master, it is a difficult point! First of all, it has a lot of meta-characters, the combination of methods are various, of course, everyone learning and understanding methods are different, in which the problem is not the same, in Linux to learn the regular expression, the first thing to learn is the use of grep and Egrep command

I'll list a few examples below!

Simple Introduction

grep, Egrep is a text search tool that can filter target text based on user-specified patterns, showing rows that are matched by patterns

Regular expressions are divided into two categories:

  1. Basic Regular Expression grep, egrep
  2. Extended Regular Expression Egrep
some options and functions of regular expressions
    • Some basic options for grep and Egrep
Options Role
-I. Ignore character casing when matching
-O Show only what matches to
-V Reverse, showing rows that are not matched to
--color Highlight the match to the content
-A Displays the following line after the matching line
-B The preceding line before the matching line is displayed
-C Show rows before and after matching rows

Basic Regular Expression (grep)
    • Character matching: A meta-character used to match characters in text
. Match any single character
[] Matches any single character within the collection
[^] Matches any single character outside the set

† . : Matches any single character

[[email protected] test]# cat Test.txtabbcaabbbcabcacbabb[[email protected] test]# grep ' a.b ' test.txt//Search Test.txt contains " AB has an arbitrary character in the middle of the line Abbcaabbbcacbabb

†[]: matches any single character in the specified collection

[[email protected] test]# cat Test.txtabbc123aabbbc343abcacb4abb[[email protected] test]# grep ' [0-9] ' Test.txtabbc123aabbbc343acb4

†[^]: matches any single character outside the set

[[email protected] test]# cat Test.txtabbc123aabbbc343abcacb4abb[[email protected] test]# grep ' [^a-z] ' Test.txtabbc123aabbbc343acb4
    • POSIX character Set
[0-9],[[:d Igit:]] Any single number within the collection
[A-z],[[:lower:]] Any single lowercase letter within the collection
[A-z],[[:upper:]] Any single uppercase letter within the collection
[A-za-z],[[:alpha:]] Any single letter within the collection
[[: Space:]] Single whitespace character
[A-za-z0-9],[[:alnum:]] Any single alphanumeric number within the collection
[[:p UNCT:]] Any single special character within the collection

†[[:d Igit:]], [0-9]: match a single number

[[email protected] test]# cat Test.txtabbc123aabbbc343abcacb4abb[[email protected] test]# grep ' [[:d igit:]] ' Test.txtabbc123aabbbc343acb4

†[[:lower:]], [A-z]: matches a single lowercase letter

[[email protected] test]# cat Test.txtabcxyzwebdb12azcabcc[[email protected] test]# grep ' [[: Lower:]] ' Test.txtabcdb12azcabcc

†[[:upper:]], [A-z]: matches a single uppercase letter

[[email protected] test]# cat Test.txtabcxyzwebdb12azcabcc[[email protected] test]# grep ' [[: Upper:]] ' Test.txtabcxyzdb12azc

†[[:alpha:]], [a-za-z]: matches a single letter

[[email protected] test]# cat Test.txt12314aab134325abc8[[email protected] test]# grep ' [[: Alpha:]] ' Test.txt14aab1abc8

†[[:alnum:]], [0-9a-za-z]: match a single digit letter, two can achieve this function

[[email protected] test]# cat Test.txt12314aab134325abc8[[email protected] test]# grep ' [[: Alnum:]] ' Test.txt12314aab134325abc8

†[[:space:]]: Match a single space

[[email protected] test]# cat test.txt12314a aadab134325abc8 131[[email protected] test]# grep ' [[: Space:]] ' test.txt14a a ADABC8 131

†[[:p UNCT:]]: That is, punctuation

[[email protected] test]# cat test.txt12314a,taab1[email protected][[email protected] test]# grep ' [[:p UNCT:]] ' Test.txt14a,ta[email protected]

†[^]: matches any single character outside the specified set and matches any non-alphanumeric character

[[email protected] test]# cat test.txtabc14a,taab1 aaa[email protected][[email protected] test]# grep ' [^[:alnum:]] ' test . TXT14A,TAAB1 Aaa[email Protected]
    • Number of times to limit the number of occurrences of a single character that is immediately preceding it.
* Matches the preceding word Fuzhin once, which means 0, 1 or more times
\? Match the characters in front of it 0 or one time
\+ Match the preceding characters to appear at least 1 times
\{m\} Match its preceding character m times, which is exactly how many times the
\{m,n\} Matches the preceding character at least m times, up to N times
    • Position anchoring
^ Anchor at the beginning of the line, appearing on the leftmost side of the pattern
$ End-of-line anchoring, appearing at the far right of the pattern
\< or \b The first anchor of the word, the leftmost side of the word pattern you are looking for
\> or \b

The ending anchor, out of the right side of the word pattern you are looking for

Extended Regular expression (egrep)
    • Character matching
. Match a single character
[] Matches a single character in a collection
[^] Match a single character outside the collection
    • Number of Matches
* Match one of its preceding words Fuzhin once
? Match its preceding character 0 or 1 times
+ Match the characters in front of it at least 1 times
{m} Match its preceding characters m times
{M,n} Match the characters in front of it m-n times
{m,} Match the characters in front of it at least m times
{0,n} Match the characters in front of it 0-n times

An extended regular expression simply removes the \ number of the regular expression, the other usage is the same, \ plays a translation in the pattern, and in the extended regular expression it is translated by default and does not need to be translated in the notation

    • Position anchoring
beginning anchor TD valign= "Top" width= "332" > /table>
^
$ < Span style= "FONT-SIZE:16PX; Font-family: in italics; " > line end anchor
 \ < or \b
\> or \b td> final anchor

The positional anchoring of an extended regular expression is also the same as the regular expression, where \ cannot be omitted

    • Group

(): Also does not require the translator \, as well as the regular expression supports the back reference \1,\2\3

Or:

    A|b:a or B

Ab|cd:ab or CD

This is the meta-character that is unique to the extended regular expression, which means "or", you need to be aware of the "|" The left and right sides of the symbol are a whole

Well, the common use of regular expressions is summed up here to sleep will feel ~ ~ ~

  

Regular expressions for Linux grep, Egrep

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.