Regular Expression -- grep

Source: Internet
Author: User

Regular expressions are used in Linux, windows, and programming languages. This is sufficient to describe the importance of regular expressions, today, let's get familiar with the regular expression of grep in Linux.

First, what is a regular expression: a regular expression is a set of rules and methods defined to process a large number of strings. Regular Expressions can be used to quickly filter and replace the required content. Linux policy expressions are handled in the unit of action.


Second, why do you learn Regular Expressions: Linux O & M work is always faced with the output of a large number of logs, programs, and commands. Mastering regular expressions can improve your work efficiency.


Grep-based Regular Expressions:

1. ^: ^ word indicates the content starting with word.

2. $: Word $ indicates the content ending with word.

3. ^ $: $ indicates the end of the article, and ^ $ indicates the content starting with the end, meaning that the blank line starts

Example: grep "^ $" test indicates that all the empty rows in the test file are output.

Grep-V "^ $" test indicates all rows in the output test file that do not contain empty rows.

4 ..: represents and can only represent any character

Example: grep "old. Y" test indicates to search for the words old and Y in the test file.

Grep ". $" test indicates to end the test file with any character.

5. \: \ stands for escape characters, so that characters with special identities can be removed from the vest to restore the prototype.

Example: \. The point here does not represent any character, but a common point.

Grep "\. $" test indicates to filter the rows ending with a vertex in the test file.

6. *: indicates to repeat 0 or multiple previous characters.

Example: "0 *" indicates repeating the line with the first 0 characters.

7 .. *: indicates that all characters are matched.

Example: grep ". *" test indicates to filter rows that repeat any character before.

8. [ABC]: Indicates matching any character in the character set.

9. [^ ABC]: ^ indicates a non-string that does not contain A, B, or C.

Grep "[^ 0-9]" Test Matches rows that are not numbers.

10. {n, m}: indicates to repeat n to m times the previous character


# End


Appendix:

Grep parameters:

Grep-V indicates that the matching content is excluded.

-E supports extended regular expressions.

-I case-insensitive

-O indicates that only Matching content is output.

-N: display the row number

This article from the "Li Yunna ---- It nvwa" blog, please be sure to keep this source http://liyunna.blog.51cto.com/9151125/1550086

Regular Expression -- grep

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.