Linux Regular expressions

Source: Internet
Author: User

Regular Expressions (Regular expression) are a standard for string processing, and for system administrators, regular is a compulsory skill. For example: The system generates too much data, as a system administrator to see so much information every day, from thousands of rows of data to find a row of problematic information, how to do? This time, we can through the function of regular expression, these login information processing, only to take out the problem of information for analysis, so that your system management work will be more intuitive, easy!

The commands we use frequently in regular expressions are the Three musketeers: grep, sed, awk. The three Musketeers are usually combined with regular special characters to accomplish the operations we need.


Options and Parameters:-A: The following can be added to the number, for after the meaning, in addition to listing the row, the subsequent n rows are also listed;-B: The following can add a number, for befer meaning, in addition to listing the row, the preceding n rows are also listed;  Lor=auto can list colors for the correct fetch data (use alias grep= ' grep--color=auto ', add to ~/.BASHRC Permanent)-I: Ignores the case of matching characters;-V: Prints rows that are not matched;-N: Output line number;-O : Prints only matching fields instead of rows, used to count how many times to match to-e: This is equivalent to Egrep, using an extended regular expression

After playing with the grep common options, combine grep to see the special characters commonly used by the underlying regular.


Regular expression special character induction


    • ^word meaning: the string to be searched (word) at the beginning of the line!

Example: Searching for the line starting with the beginning of #, parallel travel number

Grep-n ' ^# ' filename


    • word$ meaning: the string to be searched (word) at the end of the line!

Example: End of line is! , and the number of travel

Grep-n '!$ ' filename

    • ^$ meaning: blank line, after the beginning is the end of course is empty line!


    • . Meaning: "Must have an arbitrary byte" character!

Example: The search string can be (Eve) (Eae) (EEE) (e e), but not only (EE)! That is, E and E in the middle "must" only have one byte, and the blank byte is also a byte!

Grep-n ' e.e ' filename


    • \ Meaning: Jump off characters, remove special meaning of special symbols!

Example: Search for the line containing the single quote '!

GREP-N \ ' filename


    • * Meaning: Repeat 0 to infinitely multiple of the previous RE character

Example: Find a String containing (es) (Ess) (ESSS) and so on, note that because * can be 0, ES is also compatible with the search string. In addition, because * is a repetition of the "previous re character" of the symbol, so, before the * must be connected to a RE-character Fu Yi! For example, any byte is ". *"!

Grep-n ' ess* ' filename


    • [List] Meaning: The byte collection of the RE character, which lists the bytes you want to retrieve!

Example: Search for a line containing (GL) or (GD), it is necessary to pay special attention to [] in the "to represent a byte to be searched", such as "A[afl]y" for the search of the string can be Aay, Afy, Aly that [AFL] for a or F or l mean!

Grep-n ' g[ld] ' filename


    • [N1-n2] Meaning: The byte collection of the RE character, which lists the range of bytes you want to retrieve!

Example: Search for the line that contains any number! Pay special attention to the minus sign in the byte set []-it's special, he represents all the contiguous bytes between two bytes! But this continuity is related to ASCII encoding, so your coding needs to be configured correctly (in bash, you need to determine if LANG and LANGUAGE variables are correct!) For example, all uppercase bytes are [a-z]

Grep-n ' [A-z] ' filename


    • [^list] Meaning: The byte set of the RE character, which lists no strings or ranges!

Example: The search string can be (Oog) (Ood) but not (oot), that ^ within [], the meaning of the representation is "reverse selection" meaning. For example, I don't want to capitalize bytes, then [^a-z]. However, it is important to note that if you search by Grep-n [^a-z] Regular_express.txt and find all the lines in the file are listed, why? Because this [^a-z] is a "non-uppercase byte" meaning, because each row has a non-uppercase byte, for example, the first line of "Open Source" has p,e,n,o .... And so on, little handwriting.

Grep-n ' oo[^t] ' filename


    • \{n,m\} Meaning: "Previous RE character" for successive N to M

Meaning: If \{n\} is the previous RE character of a continuous n

Meaning: If \{n,\} is a continuous n more than the previous RE character! Example: A string of 2 to 3 o existence between G and G, i.e. (GOOG) (Gooog)

Grep-n ' go\{2,3\}g ' filename


    • \< and \> meanings:

      The character after the \< must be the first part of a word;

      \> the preceding character must be the last part of a word;

      Note: A word defines a combination of letters, numbers, underscores

      So \<word\> must be separate word words to match, other symbols such as: ()/etc will be treated as Word division number

    • Grouping \ (\) Meaning:

      The contents of the parentheses as a whole, such as \ (ab\) * Match 0 to Infinity AB

      Example: Test file contents are as follows:

      Love:lover

      Love:liker

      Like:lover

      Like:liker

      How do I remove rows 1 and 4?

      When using the grouping function, we can \1 \2 \3 ... To reference, so:

      grep ' \ (. *\): \1r ' Test


This article is from the "Sweetsmile" blog, make sure to keep this source http://daizhancheng.blog.51cto.com/9708457/1632045

Linux Regular expressions

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.