[Linux 006]--grep and regular expressions

Source: Internet
Author: User
Tags egrep

When using the system, we have more or less the need to search and find, we must search for a keyword in the text, or filter out some specific lines in the text. The grep command gives us such a feature, and grep can also use regular expressions to match, which is a powerful feature that needs to be mastered.

1.grep First Experience

grep pattern [OPTIONS] File: Finds in files by pattern. File is the object that we are looking for, and if you do not specify a target file, grep reads the input from the standard input and then matches it. For convenience, all of the demos in this article are performed on the command line through standard input.

    • grep pattern: The most basic usage, based on the PATTERN to find

If the match is not highlighted, you can specify it manually: grep--color PATTER to match, and you can use the command alias to reduce our input time: alias grep= "grep--color".

    • grep pattern-i: Ignoring case matching
    • grep pattern-v: Invert output. The match to the content is not output, only the content is not matched to the output.

    • grep pattern-o: Displays only the matched string (PATTERN). The default displays the entire line of text that matches the string.
2.grep and regular Expressions

The regular expression (Regular expression or REGEXP) is divided into the underlying regular expression (basic REGEXP) and an extended regular expression (Extended REGEXP). grep defaults to the underlying regular expression, which requires additional options if you want to use an extended regular expression.

Because the introduction of regular expressions is too complex, I think for a long time also can not be very good organization of language, so here is no longer the metacharacters in the regular one by one of the introduction. Students who need to know about this can take a look at the tutorial on the 30-minute introductory regular expression series, which should be learned soon.

    • Use grep for simple regular matching

"[]" means any one of the characters appearing in the match [], [a-za-z] means matching all the English letters.

"[^]" means any character that matches the character that appears in [].

  

In a regular expression, the ' * ' no longer represents any character, and the character preceding it can appear any number of times. Sometimes we use ' * ' in a command to match a wildcard character to a file, so be aware of its different uses in regular expressions. Use the '. ' point number in a regular representation to match any character.

In the regular, the '. ' point number represents any character that is not empty.

". *" means any character that matches any number of occurrences. The regular expression is in greedy mode by default, so after the first match to the result, multiple matches are made, knowing that there is no match.

"\?" means that the preceding characters appear 0 or 1 times, note that the question mark needs to be escaped.

  

"\{m,n\}" indicates that the preceding character appears at least m times, maximum n times. "\{m,\}" indicates that the preceding character appears at least m times, and "\{,n\}" indicates that the preceding character appears at most n times.

  

' ^ ' and ' $ ' are anchored in two positions in a regular expression. ' ^ ' means that the character after it must appear at the beginning of the line, while ' $ ' indicates that the character before it must appear at the end of the line, "^$" means matching white space characters.

"\<" and "\>" are used to anchor words, they do not have to appear in pairs, or they can be anchored using "\b". "\bword" (or "\<word\>") represents a word that matches the word that begins with words, and "word\b" matches words that end in Word, and "\bword\b" means that the word word is completely matched.

"\ (\)" means grouping PATTERN, grouping also supports back reference, using "\1", "\2" and so on to refer to the preceding groupings. A back reference matches only the same content as before.

' | ' to indicate or, match its previous or subsequent words, note that it is not a matching character, and that you need to qualify with a grouping if you want to match characters. I'm using the extended regular, if I need to use the basic regular, I need to change ' | ' to "\|".

3. Extending the regular expression

GREP also supports the extension of regular expressions, in which the-e option is required to use extended regular expressions. Some differences between the underlying regular expression and the extended regular expression:

    • Use \? In the basic regular, use in extension regular?
    • Use \ (\) in the base regular, extended regular use ()
    • Use \{\} in the base regular, using {} in the extended regular

Match in the underlying regular expression, (), {}, do not need to be transferred, and in an extended regular expression you need to add the escape symbol \.

Extend other options in regular expressions:

    • \s: Match whitespace characters
    • \s: Match non-whitespace characters
    • \w: Matches letters, numbers, equivalent to [0-9a-za-z]
    • \w: does not match letters and numbers, equivalent to [^0-9a-za-z]
other options for 4.grep
    • grep PATTERN file-a NUM: After a match with grep, a matching row is displayed and the specified row is displayed backwards
    • grep PATTERN file-b NUM: Displays the specified row backwards, as opposed to above
    • grep PATTERN file-c Num: After a match, the specified row is displayed forward and backward respectively, which is equivalent to-ab num
    • grep PATTERN file-c: Displays the number of rows that are matched to
    • grep PATTERN file-f: equivalent to Fgrep
    • grep PATTERN file-p: Using Perl-style regular expressions
      • \d: Matches any decimal number, equivalent to [0-9]
      • \d: Matches any non-numeric character equivalent to [^0-9]
5.egrep and Fgrep

When using grep, if you want to turn on an extended regular expression, you need to specify the-e option, and the EGREP command supports extended regular expressions by default. Therefore, it is recommended to use EGREP instead of GREP-E when you need to use an extended regular expression.

Fgrep represents a quick match, does not support regular expressions, has no escaping concept, and matches all characters in PATTERN.

6. Summary

This article introduces the basic use of GREP commands and regular expressions, and grep is a text-processing command under Linux that works with regular expressions and is very powerful. grep is primarily used to find operations, and if you want to use more powerful features such as replacements, you need to use SED or awk commands. Grep,sed,awk is called Linux three text processing kill device, and so on to learn the corresponding chapters to continue to introduce:)

Charleylla Reprint Please specify the Source: http://www.cnblogs.com/charleylla/p/5988885.html

[Linux 006]--grep and 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.