Examples and explanations of Linux grep, egrep command usage

Source: Internet
Author: User
Tags grep regular expression egrep

First, grep introduction

(1). grep (Global search Regular expression (RE) and print out of the line), [Full search of regular expressions and printing lines] is a powerful text search tool that uses regular expressions to search for text and The matching lines are printed out.

(2). The grep family includes grep, Egrep, and Fgrep, where egrep and Fgrep commands are only a small difference from grep. Egrep is an extension of grep that supports more (RE) metacharacters.

(3) can be used for Shell scripting, because grep describes the status of the search by returning a status value, if the template search succeeds, returns 0 if unsuccessful, returns 1 if the search does not exist, returns 2, and we can do some automated text processing with the return value.


(4) Syntax Description:

grep [Options] PATTERN [FILE ...]

  • -A or--text do not ignore binary data.

  • -A or--after-context= in addition to displaying the column that conforms to the template style, and displays the contents after that column.

  • -B or--byte-offset indicates the bit number of the first character of the column before displaying the column that conforms to the template style.

  • -B or--before-context= in addition to displaying the column that conforms to the template style, and displays the contents before the column.

  • -C or--count calculates the number of columns that conform to the template style.

  • -C or--context= or--in addition to displaying the column that conforms to the template style, and displays the contents before the column.

  • -D or--directories= You must use this parameter when you specify that you want to find a directory instead of a file, otherwise the GREP directive returns information and stops the action.

  • -E or--regexp= specifies the string as the template style for finding the contents of the file.

  • -E or--extended-regexp uses the template style as an extended normal notation.

  • -F or--file= specifies a template file with the contents of one or more template styles that let grep find the contents of the file that match the template criteria, in the form of a template style for each column.

  • -F or--fixed-regexp treats the template style as a list of fixed strings.

  • -G or--basic-regexp use the template style as normal notation.

  • -H or--no-filename does not indicate the file name that the column belongs to until it displays the column that conforms to the template style.

  • -H or--with-filename indicates the file name that the column belongs to before it displays the column that conforms to the template style.

  • -I or--ignore-case ignores the difference in the case of characters.

  • -L or--file-with-matches lists file names that match the file contents to the specified template style.

  • -L or--files-without-match lists file names that do not conform to the specified template style.

  • -N or--line-number indicates the column number of the column before displaying the column that conforms to the template style.

  • -Q or--quiet or--silent does not display any information.

  • -R or--recursive the effect of this parameter is the same as specifying the "-D recurse" parameter.

  • -S or--no-messages does not display an error message.

  • -V or--revert-match reverse lookup.

  • -V or--version displays version information.

  • -W or--WORD-REGEXP displays only the columns that match the whole word.

  • -X or--LINE-REGEXP displays only columns that are eligible for all columns.

  • -Y the effect of this parameter is the same as specifying the "-i" parameter.

  • --help online Help.


grep regular expression meta-character

^ Start of anchoring lines for example:' ^grep ' matches all lines that begin with grep.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/57/92/wKioL1SekTnyZRdXAABfy0dDz0Y605.jpg "title=" 1.png " alt= "Wkiol1sektnyzrdxaabfy0ddz0y605.jpg"/>


$ anchor the end of the line for example:' grep$ ' matches all lines that end with grep.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/57/92/wKioL1Sekhjz12NOAABf7X4PVTA942.jpg "title=" 1.png " alt= "Wkiol1sekhjz12noaabf7x4pvta942.jpg"/>


. Matches a non-newline character such as: ' GR.P ' matches the GR followed by an arbitrary character, followed by P.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/92/wKioL1Sek1-TcY0XAABdTHTl5eE410.jpg "title=" 2.png " alt= "Wkiol1sek1-tcy0xaabdthtl5ee410.jpg"/>


* Match 0 or more previous characters for example: ' *grep ' matches all one or more spaces followed by the grep line,. * Together, representing any character.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/57/95/wKiom1SelReQNIv_AAClEf8sRJs235.jpg "title=" 3.png " alt= "Wkiom1selreqniv_aaclef8srjs235.jpg"/>


[] matches a specified range of characters such as: ' [gg]rep ' matches grep and grep.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/57/92/wKioL1Sel3iTSRT8AAA-1qieyZE214.jpg "title=" 4.png " alt= "Wkiol1sel3itsrt8aaa-1qieyze214.jpg"/>

[^] matches a character that is not within the specified range, for example: ' [^a-eg-z]rep ' match does not contain a-eg-z beginning, immediately following Rep's line

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/57/92/wKioL1SemP_h3zKzAABKzmKoJeA003.jpg "title=" 5.png " alt= "Wkiol1semp_h3zkzaabkzmkojea003.jpg"/>


This article is from the "Chun Blog" blog, please be sure to keep this source http://ssc4469.blog.51cto.com/6315913/1596616

Examples and explanations of Linux grep, egrep command usage

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.