Linux Command Text Search tool grep, Egrep, Fgrep

Source: Internet
Author: User
Tags egrep

Linux Command Text Search tool grep, Egrep, Fgrep


grep, the global search regular Expression_r (RE) and print out, is a powerful text search tool that uses regular expressions to search for text and print matching lines.

The grep family includes grep, Egrep, and Fgrep:egrep are the extensions of grep; Fgrep finds one or more rows in a file that matches the given string or phrase, does not support regular expressions


Syntax: grep [OPTIONS] PATTERN [FILE ...]

Common parameter Options

--color=auto: Highlighting the matched string

-C: Output only the number of matching rows

-I: Case insensitive

-O: Show only strings that can be matched to a pattern

-V: Show all rows that do not match

-e: Allow use of extended pattern matching

Meta-character set pattern matching, common basic meta characters have the following:

.: Match one character

*: matches any character

[]: Matches a character within a specified range

[^]: matches a character that is not within the specified range

[[: Upper:]]:[a-z]

[[: Lower:]]:[a-z]

[[:d igit:]]:[0-9]

[[: Alnum:]]:[0-9a-za-z]

[[: Space:]]: space or TAB key

[[: Alpha:]]:[a-za-z]

\(.. \): Tag matching characters

^: Anchoring the beginning of the line

$: Anchor Line End

\<: Anchor Word first

\>: Anchor ending

X\{m\}: Repeat character x,m times

X\{m,\}: Repeat character x, at least m times

x\{m,n\}: Repeat character x, at least m times, no more than n times

\ (\): Grouping

Back to reference

\1: Refers to the first opening parenthesis and all the contents of the corresponding closing parenthesis

[2]

[3]

Example: Display a line of any single character or any character following the AB string in the Abc.log file in the/tmp/log/directory

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/57/wKiom1XhR_WDzQwJAAKGmwR_YiU591.jpg "style=" float: none; "title=" 11.jpg "alt=" Wkiom1xhr_wdzqwjaakgmwr_yiu591.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/53/wKioL1XhShvwUyGNAAJwup7C0DY442.jpg "style=" float: none; "title=" 12.jpg "alt=" Wkiol1xhshvwuygnaajwup7c0dy442.jpg "/>

Displays the line of the letter X after AB in the Abc.log file in the/tmp/log/directory, and is case-insensitive

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/57/wKiom1XhSCPDFRgtAAJVFIuQnKU157.jpg "title=" 13.jpg "alt=" Wkiom1xhscpdfrgtaajvfiuqnku157.jpg "/>

Displays the line of any letter in the Abc.log file at the beginning of the/tmp/log/directory, and is case-insensitive

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/53/wKioL1XhSl3RFFCdAAHxnbRpB1Q925.jpg "title=" 14.jpg "alt=" Wkiol1xhsl3rffcdaahxnbrpb1q925.jpg "/>

Show lines ending in bash in the/etc/passwd file

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/57/wKiom1XhSFvR6b6lAAHW5W7iQwA469.jpg "title=" 15.jpg "alt=" Wkiom1xhsfvr6b6laahw5w7iqwa469.jpg "/>

Displays the two-digit or three-digit number in the/etc/passwd file

The first of these methods

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/57/wKiom1XhSI7SbBjAAAbivcoVVm0543.jpg "title=" 16.jpg "alt=" Wkiom1xhsi7sbbjaaabivcovvm0543.jpg "/>

The second method: note that the red border in the figure

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/72/53/wKioL1XhS1-i6YSaAAac9NNuTF0856.jpg "title=" (1). jpg "alt=" wkiol1xhs1-i6ysaaaac9nnutf0856.jpg "/>

Displays lines that end with ' LISTEN ' followed by 0, one, or more whitespace characters in the ' Netstat-tan ' command result

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/53/wKioL1XhS4Ljf1boAAGQx9VL524077.jpg "title=" 17.jpg "alt=" Wkiol1xhs4ljf1boaagqx9vl524077.jpg "/>

Add user bash, Testbash, basher, and Nologin user (Nologin the user's shell is/sbin/nologin), and then find the row in the/etc/passwd file that has the same user name as its shell name

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/57/wKiom1XhSZHjGBXnAAPQr3lnY6s301.jpg "title=" 18.jpg "alt=" Wkiom1xhszhjgbxnaapqr3lny6s301.jpg "/>

Displays the default shell and UID for root, CentOS, or User1 users on the current system (please create these users beforehand, if not present)

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/57/wKiom1XhSbjjRvCCAAID4YT9xZA913.jpg "title=" 19.jpg "alt=" Wkiom1xhsbjjrvccaaid4yt9xza913.jpg "/>

Find a word in the/etc/rc.d/init.d/functions file (the middle of the word can be underlined) followed by a set of parentheses on the line

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/54/wKioL1XhTC2gRqH0AAJVBh5vNE8684.jpg "title=" 20.jpg "alt=" Wkiol1xhtc2grqh0aajvbh5vne8684.jpg "/>

Egrep command execution effect is similar to GREP-E, the syntax and parameters used can refer to the grep instruction above, the difference between grep is the method of interpreting the string, the following meta-character set

Syntax: Egrep [OPTIONS] PATTERN [FILE ...]

Common parameter Options

--color=auto: Highlighting the matched string

-C: Output only the number of matching rows

-I: Case insensitive

-O: Show only strings that can be matched to a pattern

-V: Show all rows that do not match

-e: Allow use of extended pattern matching

Common basic meta characters have the following:

.: Match one character

*: matches any character

[]: Matches a character within a specified range

[^]: matches a character that is not within the specified range

[[: Upper:]]:[a-z]

[[: Lower:]]:[a-z]

[[:d igit:]]:[0-9]

[[: Alnum:]]:[0-9a-za-z]

[[: Space:]]: space or TAB key

[[: Alpha:]]:[a-za-z]

\(.. \): Tag matching characters

^: Anchoring the beginning of the line

$: Anchor Line End

\<: Anchor Word first

\>: Anchor ending

X{M}: Repeat character x,m times

X{m,}: Repeat character x, at least m times

X{m,n}: Repeat character x, at least m times, no more than n times

(): Group

Back to reference

\1: Refers to the first opening parenthesis and all the contents of the corresponding closing parenthesis

[2]

[3]

Note: Some characters are slightly different

Example: Compare to the examples listed above

Displays the two-digit or three-digit number in the/etc/passwd file

The first of these methods

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/54/wKioL1XhTPKzc3rfAAZvPX4zybY296.jpg "title=" 16-1. JPG "alt=" wkiol1xhtpkzc3rfaazvpx4zyby296.jpg "/>

The second method of

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/72/57/wKiom1XhS0SS8OGkAAZDScv7kGI123.jpg "title=" 16-(1) . jpg "alt=" wkiom1xhs0ss8ogkaazdscv7kgi123.jpg "/>

Displays lines that end with ' LISTEN ' followed by 0, one, or more whitespace characters in the ' Netstat-tan ' command result

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/57/wKiom1XhS4OgLPACAAF0U3sHjaE750.jpg "title=" 17-1. JPG "alt=" wkiom1xhs4oglpacaaf0u3shjae750.jpg "/>

Add user bash, Testbash, basher, and Nologin user (Nologin the user's shell is/sbin/nologin), and then find the row in the/etc/passwd file that has the same user name as its shell name

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/54/wKioL1XhTf3wyBFmAAGWCG_j_mY256.jpg "title=" 18-1. JPG "alt=" wkiol1xhtf3wybfmaagwcg_j_my256.jpg "/>

Displays the default shell and UID for root, CentOS, or User1 users on the current system (please create these users beforehand, if not present)

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/57/wKiom1XhTAHg_Z1sAAEDZp_SVNo261.jpg "title=" 19-1. JPG "alt=" wkiom1xhtahg_z1saaedzp_svno261.jpg "/>

Find a word in the/etc/rc.d/init.d/functions file (the middle of the word can be underlined) followed by a set of parentheses on the line

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/54/wKioL1XhTjOR9DdfAAJSyJGvvTE739.jpg "title=" 20-1. JPG "alt=" wkiol1xhtjor9ddfaajsyjgvvte739.jpg "/>

Use echo to output a path, then egrep to find its path base name, and further use Egrep to remove its directory name

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/57/wKiom1XhThXBtLlkAAFT7sywND8149.jpg "title=" 21.jpg "alt=" Wkiom1xhthxbtllkaaft7sywnd8149.jpg "/>

Find the number between 1-255 in the ifconfig command execution result

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/54/wKioL1XhUGHBqod9AAM1VdPbBMI906.jpg "title=" 22.jpg "alt=" Wkiol1xhughbqod9aam1vdpbbmi906.jpg "/>


FGREP command: Retrieves a fixed string, which does not recognize a regular expression, is a quick search command.

Syntax: fgrep [option]pattern [FILE] ...

Common parameter options refer to grep or egrep

Example: Searching for lines with XY characters under/tmp/log/abc.log, including case

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/57/wKiom1XhTmOBHp2eAAHAl-HzBgU769.jpg "title=" 23.jpg "alt=" Wkiom1xhtmobhp2eaahal-hzbgu769.jpg "/>


This article is from the "10,000-hour Law" blog, be sure to keep this source http://daisywei.blog.51cto.com/7837970/1689526

Linux Command Text Search tool grep, Egrep, Fgrep

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.