Linux basic command grep

Source: Internet
Author: User

1. Grep meaning:

Grep is combined by g/RE/p, where g indicates global, RE is short for regular expression, and p indicates print; that is, grep indicates "global search for regular expressions and print the row" (The grep name originated from the ex editor ).
 
Grep works like this. it searches for string templates in one or more files. If the template contains spaces, it must be referenced. All strings after the template are treated as file names. The search result is sent to the screen without affecting the content of the original file.
 
2. Grep family:

The grep family includes grep, egrep, and fgrep, where egrep and fgrep are simple variants of grep.
 
Egrep extends grep and supports more regular expression metacharacters.
 
Fgrep is called fixed grep or fast grep. It treats all characters as the literal value, that is, the regular expression metacharacters are no longer special characters.
 
Note: The regular expression metacharacters supported by the grep command are the same as those supported by the grep command in the GNU version of Ubuntu13.04. During use, grep interprets the extended characters of regular expressions in the string as characters rather than the extended metacharacters of Regular Expressions unless escaped; by default, egrep interprets the extended characters contained in the string as the extended metacharacters of the regular expression. The metacharacters of the regular expression do not need to be escaped, which is the real difference between egrep and grep, rather than the character set of the supported regular expressions. Find the man command, and we have obtained confirmation:
 
-E, -- extended-regexp
Interpret PATTERN as an extended regular expression (see below ).

For example, you can try the following sentence to know the difference between grep and egrep.
 
Grep-E '2017 | 100' datafile
 
Grep '2017 \ | 100' datafile
 
Grep '2017 | 100' datafile
 
3. Grep options:
 
Matching control:
 
-E # specify the string as the style for searching the file content.
 
-F # specify the rule file, which contains one or more rule styles.
 
-I # ignore the differences in Case sensitivity.
 
-V # displays all rows that do not contain matched text.
 
-W # Only displays columns that match all characters.
 
Output Control:
 
-C # Only outputs the Count of matched rows.
 
-L # list file names that do not match the specified style.
 
-L # list the names of objects whose contents match the specified Style
 
-N # indicates the number of columns in the row before the row that matches the style is displayed.
 
-H # indicates the name of the file to which the row conforms to the style.
 
-H # The name of the file to which the row conforms to the style is not displayed.

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.