Linux linux grep command usage and regular expression

Source: Internet
Author: User
Tags gopher egrep

I. Introduction to GREP commands and regular expressions

1, grep (Global search REgular expression and print out of the line), that is, global searching regular expression and print out the matching row, it is a powerful text Search tool in Linux system, it according to user-specified "mode ( pattern) "Filters the target text to show rows that are matched by the pattern;

2. Regular expressions are patterns 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 pass-through.


Second, the basic syntax format of the grep command

grep [OPTION] ... Common options for ' PATTERN ' File...grep:-V: To reverse the matching rows-O: Show only what matches to------Ignore character case-N: Add line number to matched line-e: Use extended regular expression, equivalent to Egrep command-F : Do not use regular expression search, equivalent to the Fgrep command-A #: Shown 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, #代表任意数字--colo R=auto: Displays the matching content in a different color


Iii. basic usage of grep regular expressions

Basic Regular Expressions:

1. Character Matching

.: Matches any single character

For example: Match starts with R, and the end of T is only two characters across the line [[email protected] ~]# grep ' r. T '/etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologinftp:x:14:50:ftp User:/ Var/ftp:/sbin/nologin[[email protected] ~]#


[]: 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] uppercase: [[: Upper:]] or [A-z] Upper and lowercase: [[: Alpha:]] or [a-za-z] number plus letter: [[ : Alnum:]] or [0-9a-za-z] blank character: [[: Space:]] punctuation: [[:p UNCT:]]
For example: match the line containing the number 0 or 2 [[email protected] ~]# grep ' [/etc/passwdroot:x:0:0:root:/root:/bin/bashdaemon:x:2:2:daemon:/] sbin:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/ sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/ Nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30 : Gopher:/var/gopher:/sbin/nologinftp:x:14:50:ftp user:/var/ftp:/sbin/nologingdm:x:42:42::/var/lib/gdm:/sbin/ Nologinavahi-autoipd:x:170:170:avahi Ipv4ll stack:/var/lib/avahi-autoipd:/sbin/nologintcpdump:x:72:72::/:/sbin/ Nologin[[email protected] ~]#


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

For example: Match a line containing characters other than 1-9 range [[email protected] ~]# grep  ' [^1-9] '  /etc/passwdroot:x:0:0:root:/ root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/ sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/ SBIN:/SBIN/SHUTDOWNHALT:X:7:0:HALT:/SBIN:/SBIN/HALTMAIL:X:8:12:MAIL:/VAR/SPOOL/MAIL:/SBIN/NOLOGINUUCP:X:10:14: Uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games :/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:ftp user:/var/ftp:/sbin/ nologinnobody:x:99:99:nobody:/:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/ Sbin/nologinsaslauth:x:499:76:saslauthd user:/var/empty/saslauth:/sbin/nologinpostfix:x:89:89::/var/spool /POSTFIX:/SBIN/NOLOGINSSHD:X:74:74:PRIVILEGE-SEPARATED SSH:/VAR/EMPTY/SSHD:/SBIN/NOLOGINDBUS:X:81:81: System message bus:/:/sbin/nologinrtkit:x:498:499:realtimekit:/proc:/sbin/nologinpulse:x:497:498:pulseaudio system  Daemon:/var/run/pulse:/sbin/nologinhaldaemon:x:68:68:hal daemon:/:/sbin/nologingdm:x:42:42::/var/lib/gdm :/sbin/nologinavahi-autoipd:x:170:170:avahi ipv4ll stack:/var/lib/avahi-autoipd:/sbin/nologinntp:x : 38:38::/etc/ntp:/sbin/nologinapache:x:48:48:apache:/var/www:/sbin/nologintcpdump:x:72:72::/:/sbin/nologin[[ email protected] ~]#


2, the number of matches

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

Create a Test text that contains the following: [[email protected] ~]# cat grep_test.txty->1linexy->2linexxy->3linexxxy->4lineaby- >5lineabcy->7linexyxy->8linexyxyxxxy->9linexy->10linexxx->11linexy[[email protected] ~]#
For example: match the X-letter occurrences of the line any time: [[email protected] ~]# grep ' x*y ' grep_test.txt y->1linexy->2linexxy->3linexxxy-> 4lineaby->5lineabcy->7linexyxy->8linexyxyxxxy->9linexy->10linexxx->11linexy[[email protected] ~] #注意: Any time, description X can be 0 times.


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

Example: match x characters at least 1 times [[email protected] ~]# grep ' x\+y ' grep_test.txt xy->2linexxy->3linexxxy->4linexyxy-> 8linexyxyxxxy->9linexy->10linexxx->11linexy[[email protected] ~]#


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

Example: match the X-letter occurrences 0 or 1 times of the line [[email protected] ~]# grep ' x\?y ' grep_test.txt y->1linexy->2linexxy->3linexxxy-> 4lineaby->5lineabcy->7linexyxy->8linexyxyxxxy->9linexy->10linexxx->11linexy[[email protected] ~]#


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

Example: match the X-letter occurrences of the line 2 times [[email protected] ~]# grep ' x\{2\}y ' grep_test.txt xxy->3linexxxy->4linexxxy->9line[[email Protected] ~]#


\{m,n\}: Matches the preceding character at least m times, up to n rows, and M and N represents a range [m-n]

For example: Match x letters appear at least 1 times, at most 3 rows [[email protected] ~]# grep ' x\{1,3\}y ' grep_test.txt xy->2linexxy->3linexxxy-> 4linexyxy->8linexyxyxxxy->9linexy->10linexxx->11linexy[[email protected] ~]#


3. Position anchoring

^: Anchor at the beginning of the line

For example: match the X-letter lines that appear at the beginning of the line [[email protected] ~]# grep ' ^x ' grep_test.txt xy->2linexxy->3linexxxy->4linexyxy-> 8linexyxyxxxy->9linexy->10linexxx->11linexy[[email protected] ~]#


$: End of line anchoring

For example: match the E letter that appears at the end of the line [[email protected] ~]# grep ' e$ ' grep_test.txt y->1linexy->2linexxy->3linexxxy->4lineaby- >5lineabcy->7linexxxy->9linexy->10line[[email protected] ~]#


^$: Matching Blank lines

For example: match blank lines [[email protected] ~]# grep ' ^$ ' grep_test.txt [[email protected] ~] #这个文件只有一行空白行 ...


\<: The first anchor of the word

Example: Exact match XY two letters in the first line of a word [[email protected] ~]# grep ' \<xy ' grep_test.txt xy->2linexyxy->8linexyxyxy-> 10line[[email protected] ~]#


\>: Ending anchoring

Example: Exact match XY two letters in the ending line of a word [[email protected] ~]# grep ' xy\> ' grep_test.txt xy->2linexxy->3linexxxy-> 4linexyxy->8linexyxyxxxy->9linexy->10linexxx->11linexy[[email protected] ~]#


\<\>: Match words

For example: match the line that contains the word xy [[email protected] ~]# grep ' \<xy\> ' grep_test.txt xy->2linexy->10line[[email protected] ~] #


4. Grouping

\ (\): Group matching of a string

For example: match xy word occurrences 0 or 1 times of line [[email protected] ~]# grep ' \ (xy\) \? ' Grep_test.txt y->1linexy->2linexxy->3linexxxy-> 4lineaby->5lineabcy->7linexyxy->8linexyxyxxxy->9linexy->10linexxx->11linexy[[email protected] ~]#

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:

[email protected] ~]# cat grep.txt He like his lover. He love him love. He like his liker. He like. [Email protected] ~]#
For example: Find the line of the same word before and after: [[email protected] ~]# grep ' \ (\<[[:alpha:]].*\>\) .*\<\1\> ' Grep.txt He love him love. He like. [Email protected] ~]#


Finally, the regular expression meta-character summary:

Character matching:. , [], [^]

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

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

Group matching: \ (\)


Iv. 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;

For example: Egrep equals Grep-e[[email protected] ~]# grep-e ' k|i ' grep.txt He like his lover. He love him love. He like his liker. He like. [Email protected] ~]# egrep ' k|i ' grep.txt He like his lover. He love him love. He like his liker. He like. [Email protected] ~]#


Five, 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 linux grep command usage and regular expression

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.