Linux text processing tool grep command details

Source: Internet
Author: User

I. Introduction to grep:

The text search tool performs a row-by-row search on the target file based on the text mode specified by the user to display the rows that can be matched by the mode. The use of regular expressions can achieve powerful text processing. The following example describes the regular expression.

Ii. Classification of text processing tools

Commonly used are grep, egrep, and fgrep.

Differences:

Grep: If no parameter exists, only the RE (Regular Expression) characters are output.

Egrep: equivalent to grep-E. The biggest difference between grep and grep is the Escape Character. For example, when grep matches the number of times, \ {n, m \} egrep does not need to be directly {n, m }. Egrep is convenient.

Fgrep: equivalent to grep-f, but cannot use a regular expression. All character matching functions have been removed.

3. grep parameter description:

Format: grep [OPTIONS] PATTERN (mode) [FILE...]

Common options:

-- Color = auto: displays the color parameters.

-N: Output row number.

-V: reverse match, indicating the rows that cannot be matched by the pattern. For example, if root is specified, the matching result is not root.

Example:

-O: only the specified characters matching the pattern are displayed.

-I: case-insensitive characters.

-A #: not only can match the specified character line. The specified n rows after the character can also be displayed.

 

-B #: not only can match the specified character line. The specified n rows before the character can also be displayed.

-C #: not only can match the specified character line. The specified n rows before and after the character can also be displayed.

For the convenience of defining grep -- color = auto as an alias, the alias is cgrep. Easy to use. Then you can use cgrep to display the color.

Alias cgrep = "grep -- color = auto"

4. Common Character matching and examples:

When character matching and metacharacters are used, quotation marks must be used;

Use single or double quotes.

.: Any single character refers to a point representing 1 character. This character is not special to the minute, And the symbol is also a line.

[]: Any single character in the specified range. The specified characters in the brackets can be matched and the matching range is wide.

[^]: Specifies any single character out of the range, and the above [] idea. . The common meaning is that the characters specified in parentheses cannot be matched.

  • 1
  • 2
  • 3
  • Next Page

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.