Linux grep command usage and regular expressions

Source: Internet
Author: User
Tags egrep

Introduction to 1.GREP commands and regular expressions

(1). grep (Global search REgular expression and print out of the line), which is a powerful text-search tool in the Linux system, that searches the regular expression globally and prints out the matching rows, is based on a user-specified "pattern (pattern) "Filters the target text to show rows that are matched by the pattern;

(2). A regular expression is a pattern written by a class of characters, some of which do not represent the literal meaning of a character, but rather the function of a control or a wildcard.

Basic syntax format for 2.GREP commands

grep [OPTION] ... ' PATTERN ' FILE ...

Common options for grep:

-V: Reverse the matching rows

-O: Show only what matches to

-I : ignore character case

-N: Add line numbers for matched rows

- E: Using an extended regular expression, equivalent to the EGREP command

-F: Do not use regular expression search, equivalent to the FGREP command

-A #: Displayed along with the next # line of the matching line, #代表任意数字

- B #: Displayed along with the # line of the matching line, #代表任意数字

- C #: Displayed along with the top and bottom # lines of the matching row, #代表任意数字

--color=auto: Displays the matching content in a different color

Basic usage of 3.GREP regular expressions

Basic Regular Expressions:

(1) Character matching

. : Matches any single character

Example: Match starts with R, and the end of T is only two characters in the middle of a line

650) this.width=650; "title=" clip_image001 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image001 "src=" http://s3.51cto.com/wyfs02/M01/54/79/ Wkiom1sd2u7q7q9daacg51du5uk180.jpg "width=" 358 "height="/>

[]: matches any single character in the specified collection

Common collection Representations are:

Pure numbers: [[:d Igit:]] or [0-9]

lowercase letters : [[: Lower:]] or [A-z]

Capital letters: [[: Upper:]] or [A-z]

uppercase and lowercase letters: [[: Alpha:]] or [a-za-z]

numbers plus letters: [[: Alnum:]] or [0-9a-za-z]

whitespace characters: [[: Space:]]

punctuation: [[:p UNCT:]]

Example 1: Match a line containing a number 0 or 2 (only the first half)

650) this.width=650; "title=" clip_image002 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image002 "src=" http://s3.51cto.com/wyfs02/M02/54/79/wKiom1SD2u_ Rjow2aaecwnm6lcu413.jpg "width=" 389 "height=" 136 "/>

Example 2: Match a line containing the letter R or T (contains only the first half)

650) this.width=650; "title=" clip_image003 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image003 "src=" http://s3.51cto.com/wyfs02/M00/54/79/ Wkiom1sd2u-hr7ilaaeh75srni8219.jpg "width=" 388 "height=" 137 "/>

Example 3: Match the line containing the number 0-9 (only the first half)

650) this.width=650; "title=" clip_image004 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image004 "src=" http://s3.51cto.com/wyfs02/M01/54/79/ Wkiom1sd2vdzdtj5aaehuruikie767.jpg "width=" 391 "height=" 156 "/>

[^]: matches any single character outside the specified set

Example: Match a character closest (containing only the first half) that contains a range other than 1-9

650) this.width=650; "title=" clip_image005 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image005 "src=" http://s3.51cto.com/wyfs02/M00/54/79/ Wkiom1sd2vcgxpy1aaeeprykkre997.jpg "width=" 391 "height="/>

(2) Number of matches

*: Matches any of the preceding characters appearing any time, 0, 1, or more lines

Example: Create a Test text that contains the following:

650) this.width=650; "title=" clip_image006 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image006 "src=" http://s3.51cto.com/wyfs02/M00/54/77/ Wkiol1sd24owl7ciaacuzapqjng452.jpg "width=" 391 "height=" 219 "/>

match the X-letter occurrences of the line any time:

650) this.width=650; "title=" clip_image007 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image007 "src=" http://s3.51cto.com/wyfs02/M01/54/77/ Wkiol1sd24ptmzk1aacm7mtyhem823.jpg "width=" 388 "height=" 198 "/>

\+: Matches a line whose preceding character appears 1 or more times

Example: Match the line with x word at least 1 times

650) this.width=650; "title=" clip_image008 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image008 "src=" http://s3.51cto.com/wyfs02/M01/54/77/ Wkiol1sd24stuasdaach6uykbvk410.jpg "width=" 390 "height=" 151 "/>

\? : Matches a line whose preceding character appears 0 or 1 times

Example: Match a row with an X-letter 0 or 1 times

650) this.width=650; "title=" clip_image009 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image009 "src=" http://s3.51cto.com/wyfs02/M02/54/79/ Wkiom1sd2vli9kcaaacx7uxl-tu789.jpg "width=" 391 "height=" 202 "/>

\{m\}: Matches the line whose preceding character appears m times

Example: Match a row with an X letter appearing 2 times

650) this.width=650; "title=" clip_image010 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image010 "src=" http://s3.51cto.com/wyfs02/M01/54/79/ Wkiom1sd2vlqs46-aabrma1lem4477.jpg "width=" 391 "height=" "/> "

\{m,n\}: Matches the preceding character at least m times, at most times the rows with N, and M and n represent a range m-n

Example: Match x letters appear at least 1 times, up to 3 rows

650) this.width=650; "title=" clip_image011 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image011 "src=" http://s3.51cto.com/wyfs02/M01/54/77/ Wkiol1sd24wwdmrkaacgzs5m18y344.jpg "width=" 402 "height="/>

(3) Position anchoring

^: Anchor at the beginning of the line

Example: match the X-letter appears at the beginning of the line

650) this.width=650; "title=" clip_image012 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image012 "src=" http://s3.51cto.com/wyfs02/M02/54/77/ Wkiol1sd24xcqvqraab_rvr25cg947.jpg "width=" 389 "height=" 151 "/>

$: End of line anchoring

Example: matches the E letter appears at the end of the line

650) this.width=650; "title=" clip_image013 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image013 "src=" http://s3.51cto.com/wyfs02/M02/54/77/ Wkiol1sd24asggjraacfgrjuzwm036.jpg "width=" 390 "height="/>

^$: Matching Blank lines

Example: matching a blank line

650) this.width=650; "title=" clip_image014 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image014 "src=" http://s3.51cto.com/wyfs02/M00/54/79/wKiom1SD2vSDKET_ Aaa2intmjhs842.jpg "width=" 384 "height="/>

\<: The first anchor of the word

Example: Exact match XY two letters in the first line of a word

650) this.width=650; "title=" clip_image015 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image015 "src=" http://s3.51cto.com/wyfs02/M02/54/79/ Wkiom1sd2vxrfwy5aadqixv2p64649.jpg "width=" 390 "height=" 288 "/>

\>: Ending anchoring

Example: Exact match XY two letters in the ending line of a word

650) this.width=650; "title=" clip_image016 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image016 "src=" http://s3.51cto.com/wyfs02/M02/54/77/ Wkiol1sd24eif9icaacdkbphbh0356.jpg "width=" 390 "height=" 153 "/>

\<\>: Match words

Example: Match a line containing the word xy

650) this.width=650; "title=" clip_image017 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image017 "src=" http://s3.51cto.com/wyfs02/M02/54/79/ Wkiom1sd2vaip8llaadzswzn_x8405.jpg "width=" 390 "height=" 269 "/>

(3) Grouping

\ (\): Group matching of a string

Example: matching XY single start 0 or 1 times of the row

650) this.width=650; "title=" clip_image018 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image018 "src=" http://s3.51cto.com/wyfs02/M00/54/79/ Wkiom1sd2varyiiaaacsuhxdc6i584.jpg "width=" 392 "height=" 216 "/>

Back reference: In the pattern, if you use \ (\) to implement the grouping, in a text check, if \ (\) pattern matches to a certain content, this content can be referenced in the following pattern;

The symbols that refer to the preceding groupings are: \1, \2, \3

The pattern is from left to right, referencing the first # opening parenthesis and matching the pattern between the closing parenthesis and its matching right;

Example of a back reference:

Create a new text file, assuming the following:

650) this.width=650; "title=" clip_image019 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image019 "src=" http://s3.51cto.com/wyfs02/M01/54/77/ Wkiol1sd24igoxo4aabd3nrswf0288.jpg "Width= " "height="/>

find lines with the same words before and after:

650) this.width=650; "title=" clip_image020 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image020 "src=" http://s3.51cto.com/wyfs02/M01/54/79/ Wkiom1sd2vfqzs2baabkw_vv-f0659.jpg "width=" 536 "height=" "/>

Regular expression meta-character summary:

character matching:., [], [^]

Number of matches: *, \?, \+, \{m\}, \{m,n\}

Location Anchoring: ^, $,\<,\>,\<\>

Group matching: \ (\)

4.egrep and extended Regular expressions:

Egrep equivalent to Grep-e,egrep can use extended regular expressions directly, and grep needs to add an option-E;

Extend the metacharacters of regular expressions:

character matching:., [], [^]

number of matches: *,?,+,{m},{m,n},{m,},{0,n}

position Anchor:^,$,\>,\<

Group Matching: (), support for back reference

|: Matches the line on the left or right, such as a|b, with a or B line matching;

example 1:egrep equivalent to Grep-e

650) this.width=650; "title=" clip_image021 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image021 "src=" http://s3.51cto.com/wyfs02/M02/54/79/ Wkiom1sd2vfgw1fqaac35zgoezy298.jpg "width=" 389 "height=" 175 "/>

Example 2:

650) this.width=650; "title=" clip_image022 "style=" border-right-width:0px;border-bottom-width:0px; border-top-width:0px; "border=" 0 "alt=" clip_image022 "src=" http://s3.51cto.com/wyfs02/M00/54/79/ Wkiom1sd2vijhajoaabrbwyinj0018.jpg "width=" 394 "height=" 101 "/>

5.grep Practice Questions:

(1). Displays lines in the/proc/meminfo file that begin with uppercase or lowercase s;

# grep-i ' ^s '/proc/meminfo

(2). Displays the user whose default shell is non-/sbin/nologin in the/etc/passwd file;

# grep-v '/sbin/nologin$ '/etc/passwd | Cut-d:-f1

(3). Displays the user whose default shell is/bin/bash in the/etc/passwd file

Further: Displays only the user whose ID number is the highest in the above results

# grep '/bin/bash$ '/etc/passwd | Cut-d:-f1 | Sort-n-R | Head-1

(4). Find one or two digits in the/etc/passwd file;

# grep ' \<[[:d igit:]]\{1,2\}\> '/etc/passwd

(5). Display lines beginning with at least one whitespace character in/boot/grub/grub.conf

# grep ' ^[[:space:]]\+.* '/boot/grub/grub.conf

(6). Displays the line in the/etc/rc.d/rc.sysinit file, beginning with #, followed by at least one white space character, and then with at least one non-whitespace character;

# grep ' ^#[[:space:]]\+[^[:space:]]\+ '/etc/rc.d/rc.sysinit

(7). Find the line containing ' LISTEN ' in the result of the Netstat-tan command execution;

# Netstat-tan | grep ' listen[[:space:]]*$

(8). Add User Bash,testbash,basher,nologin (shell is/sbin/nologin), and find out the same user name and default shell on the current system;

# grep ' \ (\<[[:alnum:]]\+\>\). *\1$ '/etc/passwd

(9). Extension question: Create a new text file, assuming the following:

He like his lover.

He love his lover.

He like his liker.

He love his liker.

Find out that the last word is a line consisting of a previous word plus r;

# grep ' \ (\<[[:alpha:]]\+\>\). *\1r ' Grep.txt

(10). Displays the default shell and user name of the root, CentOS, or User1 user on the current system;

# grep-e ' ^ (root|centos|user1\>) '/etc/passwd

(11). Find the line after a word in the/etc/rc.d/init.d/functions file followed by a pair of parentheses ' () ";

# grep-o ' \<[[:alpha:]]\+\> () '/etc/rc.d/init.d/functions

(12). Use Echo to output a path, and use Egrep to remove its base name;

# echo/etc/rc.d/| grep-o ' [^/]\+/\?$ ' | grep-o ' [^/]\+ '

Linux grep command usage and regular expressions

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.