Linux Basic Text Search tool grep

Source: Internet
Author: User
Tags grep regular expression egrep

One, grep (abbreviated from globally search a Regular expression and print) is a powerful text-search tool that can use regular expressions to search for text and print matching lines. The grep family of Unix includes grep, egrep, and Fgrep

grep: The default support for basic regular expressions;
Egrep: extended regular expression;
Fgrep: Regular expression metacharacters are not supported, fast searching for strings

Second, get grep help information through the man manual:

#man grep

grep (1) grep (1)

NAME
grep, Egrep, fgrep-print lines matching a pattern

Synopsis
grep [Options] PATTERN [FILE ...]
grep [Options] [-E PATTERN |-f file] [FILE ...]

DESCRIPTION
Grep searches the named input FILEs (or standard input if no FILEs is named, or the file Name-is given) for
Lines containing a match to the given PATTERN. By default, grep prints the matching lines.

In addition, the variant programs Egrep and Fgrep are available.   Egrep is the same as GREP-E. Fgrep is the
Same as Grep-f.

Iii. Common options for grep

--color=auto
Export grep_color= ' 01;36 '
-V: Reverse selection, showing only rows that do not conform to the pattern;
-O: Displays only strings that are matched to the pattern, not the entire row;
-I: case-insensitive character;
-A #: Displays the row to which it was matched, showing the # lines following it, incidentally;
-A 2
-B #: line in front of #;
-C #: Before and after the # line;

-E: Using extended regular expressions
GREP-E = Egrep

C. grep regular expression basic meta-character set

Metacharacters: does not represent the meaning of the character itself, but is used for the description of the additional functionality

^: Qualifying content anchored at the beginning of the line

# grep "^root"/etc/passwd//search for lines starting with root

650) this.width=650; "title=" image "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/59/FF/ Wkiom1tx8mageib7aacumwhbtoi608.jpg "width=" 430 "height=" "/>"
$: Anchoring line endings for eligible content

#grep "bash$"/etc/passwd//search for lines ending in bash

650) this.width=650; "title=" image "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/59/FF/wKiom1Tx8mahtvRKAAFlzLFIJ_ Y687.jpg "width=" 718 "height=" 113 "/>

.: Matches any single character

650) this.width=650; "title=" image "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M01/59/FF/ Wkiom1tx8mejsljqaabjwdukhlm862.jpg "width=" 407 "height=" 118 "/>

Matches the contents of a single arbitrary character between S and L

650) this.width=650; "title=" image "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/59/FF/ Wkiom1tx8meizfo3aaa0w1mq3k8100.jpg "width=" 382 "height="/>


*: matches the character immediately preceding it any time (contains 0 times)

650) this.width=650; "title=" image "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/59/FF/ Wkiom1tx8mfjzppraaa197kztko611.jpg "width=" "height=" 104 "/>

Match k with 0 or any s in front of

650) this.width=650; "title=" image "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; margin:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M01/59/FF/wKiom1Tx8mfSMIZ0AAA8EBIf_ 1a788.jpg "width=" 383 "height=" "/>

[]: matches any single character within the specified range

Match D or D in the middle of a line containing two arbitrary characters and ending with n

650) this.width=650; "title=" image "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/59/FC/ Wkiol1tx83ehuwyjaagtfx1iaow409.jpg "width=" 973 "height="/>
[^]: matches any single character outside the specified range

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/59/FC/ Wkiol1tx83fieyeyaabqimqkh60087.jpg "" 327 "height="/>

#grep "[^a-z].*a" text--color=auto//matches any one of the lines that is not large with the letter followed by 0 or more arbitrary characters ending with a

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M01/59/FC/ Wkiol1tx83etu7moaab1x9qnmr0819.jpg "" 572 "height=" "/>
\?: matches the character immediately preceding it 0 or 1 times;

0 or 1 a before matching B

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/59/FC/ Wkiol1tx83jyobxcaactuglycai155.jpg "" 619 "height=" 224 "/>

There are 0 or 1 arbitrary characters before matching A and b

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/59/FC/ Wkiol1tx83iwjlroaabdehnpqwu846.jpg "" 630 "height=" 104 "/>

\{m,n\}: Matches its preceding character at least m times, up to n times;
\{0,n\}: Up to n times; 0-n times;
\{m,\}: At least m times
\{m\}: Exact match m times;

Match A in front of b at least 1 times up to 2 times

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M01/59/FC/ Wkiol1tx83irjstbaacecvyhbk0574.jpg "" 571 "height=" 225 "/>

\<: Anchor word First, usage format: \<pattern
\b: \bpattern

Search for lines with root as the first word

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M01/5A/00/ Wkiom1tx8mqqhweuaadfxi3ysyw567.jpg "" 618 "height=" "The/>

\>: Anchor ending, usage format:pattern\>
\b:pattern\b

Search for a line ending in root

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/5A/00/ Wkiom1tx8mqgan89aaduvhvpps8557.jpg "" 591 "height="/>
\<pattern\>: Anchoring words

Search for lines that contain root words

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/5A/00/ Wkiom1tx8mrspid1aacxsqgcvj8824.jpg "" 626 "height="/>

\ (\): Group, usage format: \ (pattern\)

\1: You can refer to what the first grouping matches
\2: You can refer to what the second grouping matches


Search for two arbitrary characters between R or R and D followed by 0 or more arbitrary characters, followed by references to matched content

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M01/5A/00/ Wkiom1tx8muzkupraaeefs66li4136.jpg "" 626 "height=" 138 "/>

Character Set fit

[:d igit:] All numbers 0-9

[: Lower:] All lowercase letters A-Z

[: Upper:] All capital letters A-Z

[:p UNCT:] All punctuation

[: Space:] Indicates a space or TAB key

[: Alpha:] denotes all letters (including capitalization) a-za-z

[: Alnum:] denotes all letters and numbers a-za-z0-9

Note: It is not necessary to represent [^[]], such as [[: space:]] means space [^[:space:]] means non-empty

Search/boot/grub/grub.conf lines that begin with a space

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; margin:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M01/59/FC/ Wkiol1tx83rwoucgaac8mozxyrg269.jpg "" 685 "height="/>

Linux Basic Text Search tool grep

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.