grep and regular expressions

Source: Internet
Author: User
Tags gopher expression engine egrep

grep and regular expressions

Grep:
The Three musketeers of text processing on Linux
grep: Text filter (Pattern: pattern) tool;
grep, Egrep, Fgrep
Sed:stream Editor, text editing tools;
Awk:linux on the implementation of Gawk, Text Report generator;

Grep:global search REgular expression and Print out of the line.
Function: Text Search tool, according to user-specified "mode" to match the target text line by row to check; print matching lines;
Pattern: The filter condition written by the regular expression character and the text character;
REGEXP: A pattern written by a class of special characters and text characters, some of which do not represent the literal meaning of a character, but are a function of control or wildcard;
Divided into two categories:
Basic Regular Expressions: BRE
Extended Regular expression: ERE
GREP-E, Egrep

Regular expression engine

grep [OPTIONS] PATTERN [FILE ...]

Options:
--color=auto: Coloring the text to match to the display;
-V: Displays rows that cannot be matched to pattern;
-I: ignore character case;
-O: Displays only the matching string;
-Q: Silent mode, do not output any information;
-A #:after, after # line
-B #: Before, Front # line
-c #:context, front and back # lines

-E: Use ere;

[[email protected] ~]# grep root/etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/ Nologin
[Email protected] ~]# grep--color=auto root/etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/ Root:/sbin/nologin
[Email protected] ~]# grep-v root/etc/passwdbin: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/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:499: "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/nologin

-I ignores case

-O displays itself only

[Email protected] ~]# Grep-o root/etc/passwdrootrootrootroot
[Email protected] ~]# grep-q root/etc/passwd[[email protected] ~]# echo $?0
[Email protected] ~]# grep-q xxxxxxxxxx/etc/passwd[[email protected] ~]# echo $?1

A match to, a no match to

-A 2 indicates that the matched row and the following 2 rows are displayed

[Email protected] ~]# grep-a 2 root/etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/ NOLOGINDAEMON:X:2:2:DAEMON:/SBIN:/SBIN/NOLOGIN--OPERATOR:X:11:0:OPERATOR:/ROOT:/SBIN/NOLOGINGAMES:X:12:100: Games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologin

-B 2 indicates matching rows and first 2 rows

[Email protected] ~]# Grep-b 2 root/etc/passwdroot:x:0:0:root:/root:/bin/bash--mail:x:8:12:mail:/var/spool/mail:/ Sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologin

-C 2 represents the matched rows and their preceding and after 2 rows

[Email protected] ~]# grep-c 2 root/etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/ NOLOGINDAEMON:X:2:2:DAEMON:/SBIN:/SBIN/NOLOGIN--MAIL: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/nologin



         Basic Regular expression meta-characters:
             Character matching:
                .   :   match any single character;
                []  : Matches any single character within the specified range
                [^]  : matches any single character outside the specified range
                    [ :d Igit:], [: Lower:], [: Upper:], [: Alpha:], [: Alnum:], [:p UNCT:], [: space:]
                     
means to find items with an S start, N end, and 2 single-character characters in the middle

[[email protected] ~]# grep  ' s. N '  /etc/passwdbin: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:499: "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/nologin 


Number of matches: used after the number of characters to be specified, to specify the number of occurrences of the preceding character;
*: matches the preceding character any time; can be 0,1,2,3 .... any
For example: grep "X*y"
Abxy
Xay
Xxxxxxy

[email protected] ~]# cat Aa.txt abxyxayxxxyxxaayaaaa
[Email protected] ~]# grep x*y aa.txt abxyxayxxxyxxaay

Greedy mode
. *: Any character of any length;

[email protected] ~]# cat Aa.txt abxyxayxxxyxxaayaaaa


[Email protected] ~]# grep x.*y aa.txt abxyxayxxxyxxaay

\?: matches the preceding character 0 or 1 times, i.e. the preceding is optional;
\+: Matches its preceding character at least 1 times;
\{m\}: Matches the preceding character m times;
\{m,n\}: Matches the preceding character at least m times, up to n times;
\{0,n\}: Matches the preceding character up to n times;
\{m,\}: Matches the preceding character at least m times;

[[email protected] ~]# grep--color=auto x*y aa.txt abxyxayxxxyxxaay

Location anchoring:
^: Anchor at the beginning of the line, for the leftmost mode;
$: End of line anchoring; for the rightmost side of the pattern;
^pattern$: Used for pattern matching whole line;
^$: Empty line;
^[[:space:]]*$

\< or \b: The first anchor of the word, the left side of the word pattern;
\> or \b: the ending anchor; for the right side of the word pattern;
\<pattern\>: matches the whole word;

Group:
\ (\): Bind one or more characters together and treat them as a whole;
\ (xy\) *ab

Note: The contents of the pattern in the grouping brackets are recorded in internal variables by the regular expression engine, which are named: \1, \2, \3, ...
\1: From the left, the first opening parenthesis and the matching closing parenthesis to match the pattern between the characters;
\ (ab\+\ (xy\) *\):
\1:ab\+\ (xy\) *
\2:xy

Back reference: References the pattern in the preceding grouping brackets to match the character (not the pattern itself)

Practice:
1. Display the line in the/proc/meminfo file that begins with the size S; (Requires: use two ways)
Grep-i ' ^s '/proc/meminfo

2. Display the lines in the/etc/passwd file that do not end in/bin/bash;
grep '/bin/bash$ '/etc/passwd
Grep-v '/bin/bash$ '/etc/passwd

3. Display the user name of the user with the largest ID number in the/etc/passwd file;
SORT-T:-k3-n/etc/passwd | Tail-l | Cut-d:-f1

4, if the user root exists, display its default shell program;
# ID Root &>/dev/null && grep "^root\>"/etc/passwd | Cut-d:-f7

5. Find out the number of two or three digits in/etc/passwd;
# grep "\<[0-9]\{2,3\}\>"/etc/passwd

6. Display a line in the/etc/rc.d/rc.sysinit file that begins with at least one white space character and that is followed by a non-whitespace character;
# grep "^[[:space:]]\+[^[:space:]]"/etc/grub2.cfg

7. Find the line ending with ' LISTEN ' followed by 0, 1, or more whitespace characters in the result of the "Netstat-tan" command;
# Netstat-tan | grep "listen[[:space:]]*$"

8, add user bash, Testbash, basher and Nologin (its shell is/sbin/nologin), and then find the/etc/passwd file user name with the shell name of the line;
Useradd-s/sbin/nologin Nologin
# grep "^\ ([[: alnum:]]\+\>\). *\1$"/etc/passwd


Practice:
1, write a script to achieve the following functions
If the User1 user exists, it is shown to exist, otherwise added;
Displays information such as the ID number of the added user;
#!/bin/bash
ID user1 &>/dev/null && echo "user1 exists." | | Useradd user1
ID User1

2, write a script to complete the following functions
If the root user is logged in to the current system, the root user is shown online, otherwise it is not logged in;
W | grep "^root\>" &>/dev/null && echo "root logged" | | echo "No Loggin"

This article is from the "Liang blog" blog, make sure to keep this source http://7038006.blog.51cto.com/7028006/1827208

grep 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.