Use of the Linux Operation _grep/egrep Tool

Source: Internet
Author: User
Tags egrep

One, the grep command introduction

Command format:grep [-cinvabc] ' word ' filename, the common options are as follows:

    • -C: Indicates the number of lines that are printed to match the requirement.
    • -I: Indicates that case is ignored.
    • -N: Indicates that the output conforms to the required line and its line number.
    • -V: Prints lines that do not meet the requirements.
    • -A: followed by a number (with or without a space), such as-A2 to print the line that meets the requirements and the following two lines.
    • -B: followed by a number, such as-B2, that prints the line that meets the requirements and the above two lines.
    • -C: followed by a number, such as-C2, to print the line that meets the requirements and two rows above and below.

Second, filter out a line with a keyword, and lose the travel number

Description: The preceding number is shown in green, indicating the line number.

Third, filter out the line without a keyword, and lose the travel number

Iv. filter out all rows that contain numbers

Note: As long as there is a number to match.

V. Filter out all rows that do not contain numbers

Note: It is not displayed as long as it contains a number.

Six, filter out all lines beginning with #

Description: This contains a blank line.

Seven, filter out all empty lines and lines beginning with #

In a regular expression, ^ represents the beginning of a row, $ represents the end of the line, and ^$ represents a blank line.

How do I print a line that doesn't start with an English letter? Examples are as follows:

Note: If you want to filter the numbers in [0-9] This form (when encountering a similar [15] in the form of the expression contains only 1 or 5). If you want to filter numbers and uppercase and lowercase letters, write a form similar to [0-9a-za-z]. In addition,[^ character] denotes characters other than [] characters.

Note: There is a difference between writing the ^ in square brackets and the outside.

Eight, filter out any one character and repeating character

. represents any one character. In the example above, R.O indicates that a line with an arbitrary character between R and O is filtered out.

* Represents 0 or more * preceding characters. in the above example, ooo* means Oo, ooo, oooo ... or more O.

In the example above,. * Represents 0 or more arbitrary characters, and a blank line is included, which matches all the lines in the/etc/passwd file.

Nine, specify the characters to filter out the number of words

Description: The symbol {}, which is a number inside, indicating the number of times the preceding character is to be repeated .

Note (focus): {} need to add the escape character \. In addition, the use of "{}" can also represent a range, the format is {n1,n2}, where N1 < N2, representing the repetition N1 to n2 the preceding character, N2 can also be empty, this represents greater than or equal to N1 times.

Ten, filter out one or more specified characters (start using the Egrep command)

Description: The EGREP command uses the symbol +, which indicates that matches 1 or more + preceding characters, this "+" does not support being used directly by the grep command; {} can be used directly by egrep without adding \ escaping. For example:

Xi. filter out 0 or one of the specified characters

12. Filter out string 1 or String 2

13, the use of Egrep ()

Description: This is used () to represent a whole, and in the example above, the rows containing rooo or Rato are filtered out.

Also note that you can combine () with other symbols, for example:

Use of the Linux Operation _grep/egrep Tool

Related Article

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.