[Linux 006] -- grep and regular expression, 006 grep

Source: Internet
Author: User
Tags egrep

[Linux 006] -- grep and regular expression, 006 grep

When using the system, we have more or less search and search requirements. We must search for a keyword in the text or filter out certain lines in the text. The grep command provides us with such a function. At the same time, grep can also use regular expressions for matching. This is a powerful function and must be well mastered.

1. grep initial experience

Grep PATTERN [OPTIONS] FILE: searches for files by mode. FILE is the target FILE to be searched. If the target FILE is not specified, grep reads the input content from the standard input and then matches it. For convenience, all the demos in this article are carried out through standard input in the command line.

  • Grep PATTERN: the most basic usage. Search by PATTERN.

If no matching content is highlighted, You can manually specify grep -- color PATTER to match the content. You can also use the command alias to reduce the input time: alias grep = "grep -- color ".

  • Grep PATTERN-I: Ignore case-insensitive matching
  • Grep PATTERN-v: reverse output. The matched content is not output. Only the unmatched content is output.

Grep PATTERN-o: only the matched string (PATTERN) is displayed ). By default, the entire line of text of the matched string is displayed.2. grep and Regular Expressions

Regular Expressions (Regular expressions or REGEXP) are divided into Basic REGEXP and Extended REGEXP ). Grep uses the basic Regular Expression by default. If you want to use an extended regular expression, additional options are required.

The introduction of regular expressions is too complex. I have been thinking about it for a long time and cannot properly organize the language. Therefore, I will not introduce the metacharacters in regular expressions one by one here. If you need to know about this, you can take a 30-minute tutorial on getting started with the regular expression series. You should be able to learn it soon.

  • Use grep for simple regular expression matching

"[]" Indicates matching any character in [], and [a-zA-Z] indicates matching all English letters.

"[^]" Indicates any character other than the character that appears in.

In a regular expression, '*' no longer represents any character, but can appear any time before it. Sometimes we use '*' in the command to match files with wildcards. Pay attention to its different usage in regular expressions. Use the '.' dot in the regular expression to match any character. In a regular expression, '.' indicates any character that is not empty.

". *" Indicates matching any number of characters. The regular expression is in greedy mode by default. Therefore, after the result is matched for the first time, it will be matched multiple times until it is known that there is no matching.

"\? "Indicates that the first character is matched 0 or 1 time. Note that this question mark must be escaped.

"\ {M, n \}" indicates that the preceding characters appear at least m times and at most n times. "\ {M, \}" indicates that the previous character appears at least m times. "\ {, n \}" indicates that the previous character appears at most n times.

'^' And '$' are two locations in the regular expression. '^' Indicates that the character after it must appear at the beginning of the line, and '$' indicates that the character before it must appear at the end of the line. "^ $" indicates that it matches blank characters.

"\ <" And "\>" are used to pin words. They do not have to appear in pairs. You can also use "\ B" to pin words. "\ BWORD" (or "\ <WORD \>") indicates matching words starting with WORD, and "WORD \ B" indicates matching words ending with WORD, "\ bWORD \ B" indicates that the WORD is completely matched.

"\ (\)" Indicates to group PATTERN. The Group also supports backward reference and uses "\ 1", "\ 2", and other references to the previous group. The backward reference only matches the same content as the previous one.

'|' Indicates or, which matches the word before or after it. Note that it is not a matching character. to match a character, use a group to limit it. Here I use an extended regular expression. To use a basic regular expression, replace '|' with "\ | ".

3. Extended Regular Expression

Grep also supports extended regular expressions. If you use an extended regular expression, you must use the-E option. Some differences between basic and extended regular expressions:

  • Use \?, Used in extended regular expressions?
  • Use \ (\) in basic regular expressions, and use () in extended Regular Expressions ()
  • Use \ {\} in basic regular expressions and {} in extended Regular Expressions {}

Matching in the basic regular expression ?, (), {}, Do not need to be transferred, but add the escape symbol \ in the extended regular expression \.

Other options in the extended regular expression:

  • \ S: matches blank characters
  • \ S: Match non-blank characters
  • \ W: matching letters and numbers, equivalent to [0-9a-zA-Z]
  • \ W: unmatched letters and numbers, equivalent to [^ 0-9a-za-z]
4. Other grep options
  • Grep pattern file-a num: After grep is used for matching, the matched rows are displayed, and the specified rows are displayed backward.
  • Grep pattern file-B NUM: opposite to the above, the specified row is displayed backward
  • Grep pattern file-c num: After matching, the specified rows are displayed both forward and backward, which is equivalent to-AB NUM.
  • Grep pattern file-c: displays the number of matched rows.
  • Grep pattern file-F: equivalent to fgrep
  • Grep pattern file-P: Use a Perl-style Regular Expression
    • \ D: matches any decimal number, which is equivalent to [0-9]
    • \ D: match any non-numeric character, equivalent to [^ 0-9]
5. egrep and fgrep

When using grep, if you want to enable the extended regular expression, you must specify the-E option, while the egrep command supports the extended regular expression by default. Therefore, we recommend that you use egrep instead of grep-E when you need to use an extended regular expression.

Fgrep indicates a quick match. It does not support regular expressions and does not support escape. It matches all characters in PATTERN.

6. Summary

This article describes the basic use of grep commands and regular expressions. grep is a text processing command in Linux. It is very powerful when used with regular expressions. Grep is mainly used for search operations. If you want to use more powerful functions such as replacement, you need to use the sed or awk command. Grep, sed, and awk are called the three text processor in Linux. We will continue to introduce them after learning the relevant chapters :)

 

 

Author: Charleylla reprint please indicate the source: http://www.cnblogs.com/charleylla/p/5988885.html

 

 

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.