6. GREP and regular expressions

One, Regular Expression

Regular Expression: Regual Expression, REGEXP.
A pattern written by a type of special characters and text characters, some of which do not express their literal meaning, but are used to express control or wildcard functions;
Basic regular expression: BRE
Extension Regular expression: ERE

1.Metacharacters of basic regular expression

1.1 Character matching:

.: Match any single character

[]: Match any single character in the specified range
[
^]: match any single character outside the specified range
[:digit:] number
[:lower:] lowercase letters
[:upper:] uppercase letters
[:alpha:] letters
[:alnum:] number
[:punct:] punctuation
[:space:] Vertical or horizontal blank characters

1.2 Match times: used after the character to be specified times, used to specify the number of times the preceding character should appear;

 *: Match the preceding character any number of times, including 0 times; work in greedy mode by default (match as long as possible)

.: any single character
.
*: Any character of any length
?: Match the preceding character 0 or 1 time
\
+: Match the preceding character at least once
\{n\}: Match the preceding character n times
\{n,m\}: Match the preceding character n to m times
  \{n,\}: Match the preceding character at least n times
  \{,m\}: Match the preceding character at most m times

1.3 Position anchor: Position the position where the character to be anchored appears

  • ^ (cart character): anchor at the beginning of the line, use On the leftmost side of the matching pattern
  • $: End-of-line anchor, used to match the rightmost side of the pattern
    • ^$: Blank line
    • ^[[:space:]]$: Blank line (blank line Or lines containing blank characters)
    • ^PATTERN$: Use PATTERN to match the entire line
  • \< or \b prefix anchor, used on the left side of the word pattern
  • \> or \b End of word anchor, used on the right side of the word pattern
  • \bPATTERN\b or matches the entire word
    Note: continuous characters (strings) composed of non-special characters are called words;< /strong>

1.4 Grouping and Quoting: \(\) Put one or Multiple characters are bundled together and processed as a whole; for example, \(root\)\+ means that root is divided into a group and matched at least once

Back quote: quote the preceding sub-characters matched by the pattern in the group brackets (not the pattern itself)

Note: the characters in the group brackets The content matched by the pattern will be expressed regularly The pattern engine is recorded in internal variables. The naming method of these variables is: \1,\2,\3

\1: The pattern starts from the left, the first The characters matched by the pattern between the left parenthesis and the matched right parenthesis;
\2: The pattern starts from the left, and the pattern between the second left parenthesis and the matched right parenthesis is matched to Characters;
\3:

Example: \(string1\+\(string2\)\)
\1:(string1+(string2)
)
\2:(string2)

\(ab\+\(xy\)*\)

\1:ab\ +\(xy\)*

\2:xy

eg

Or:|
Example:
a| b: a or b
C | cat: C or cat
(C|c) at: Cat or cat

二、 grep, egrep, fgrep

grep: Global search REgular expression and Print out the line.
Function: Text search tool, according to the user-specified “mode (filter conditions)” to the target text line by line Match check; print the matched lines;
pattern: filter conditions written by regular expression characters and text characters

grep [OPTIONS] PATTERN [FILE...]

grep [OPTIONS] [-e PATTERN | -f FILE] [ FILE...]
# option:
--color=auto: color the matched text and highlight it;
-i: ignorecase, ignore the case of characters;
-o: Only display the matched string itself;
-w: Display the entire word that matches
-v, --invert-match: Invert selection (display lines that cannot be matched by the pattern);
-e: Realize the logical or relationship between multiple options
-n: Display the matching line number
-c: count the number of matched rows
-E: Supports the use of extended regular expression metacharacters;
-q, --quiet, --silent: silent mode, that is, no information is output; -A 5: after, the last 5 lines
-B 5: before, the first 5 lines
-C 5: context, 5 lines before and after each

< h2 id="four exercises">three, exercises

1. Display the lines that do not end with /bin/bash in the /etc/passwd file;
~]# grep -v “/bin/ bash$” /etc/passwd

2, find out the two or three digits in the /etc/passwd file;
~]# grep “\<[0-9]\{ 2,3\}\>” /etc/passwd

grep “\<[[:digit:]]\{2,3\}\>” /etc/passwd

3. Find out the lines in the /etc/rc.d/rc.sysinit or /etc/grub2.cfg file that start with at least one blank character and are followed by non-blank characters;
~]# grep “^[ [:space:]]\+[^[:space:]]” /etc/grub2.cfg

4. Find out the result of the “netstat -tan” command with’LISTEN’ followed by 0, 1 or more lines ending with blank characters;
~]# netstat -tan | grep “LISTEN[[:space:]]*$”



1, find out In the /proc/meminfo file, all lines beginning with uppercase or lowercase S; there are at least three ways to achieve;
~]# grep -i “^s” /proc/meminfo
~]# grep “^[ sS]” /proc/meminfo
~]# grep -E “^(s|S)” /proc/meminfo

2, display the root, centos or user1 user related information on the Xiaoqian system Information;
~]# grep -E “^(root|centos|user1)>” /etc/passwd

3, find out the /etc/rc.d/init.d/functions file A line in which a word is followed by a parenthesis;
~]# grep -E -o “[_[:alnum:]]+()” /etc/rc.d/init.d/functions

4. Use the echo command to output an absolute path, and use egrep to retrieve the base name;
~]# echo /etc/sysco nfig/ | grep -E -o “[^/]+/?$”

Further: take out its path name; similar to the result of executing the dirname command on it;

5 , Find out the value between 1-255 in the result of the ifconfig command;
~]# ifconfig | grep -E -o “<([1-9]|[1-9][0-9]|1 [0-9]{2}|2[0-4][0-9]|25[0-5])>“

6. Homework: Find out the IP in the result of the ifconfig command Address;
~]# ifconfig | grep -o “([0-9]{1,3}.){3}[0-9]{1,3}” –color=auto

7. Add users bash, testbash, basher and nologin (the shell is /sbin/nologin); then find the line in the /etc/passwd file where the user name is the same as the shell name; ~]# grep -E “^( [^:]+>).*\1$” /etc/passwd

.: match any single character

[]: Match any single character in the specified range
[
^]: match any single character outside the specified range
[:digit:] number
[:lower:] lowercase letters
[:upper:] uppercase letters
[:alpha:] letters
[:alnum:] number
[:punct:] punctuation
[:space:] Vertical or horizontal blank characters

*: match the preceding Characters any number of times, including 0 times; the default works in greedy mode (match as long as possible)

.: any single character
.
*: Any character of any length
?: Match the preceding character 0 or 1 time
\
+: Match the preceding character at least once
\{n\}: Match the preceding character n times
\{n,m\}: Match the preceding character n to m times
  \{n,\}: Match the preceding character at least n times
  \{,m\}: Match the preceding character at most m times

grep< /span> [OPTIONS] PATTERN [FILE...]

grep [OPTIONS] [-e PATTERN | -f FILE] [ FILE...]
# option:
--color=auto: color the matched text and highlight it;
-i: ignorecase, ignore the case of characters;
-o: Only display the matched string itself;
-w: Display the entire word that matches
-v, --invert-match: Invert selection (display lines that cannot be matched by the pattern);
-e: Realize the logical or relationship between multiple options
-n: Display the matching line number
-c: count the number of matched rows
-E: Supports the use of extended regular expression metacharacters;
-q, --quiet, --silent: silent mode, that is, no information is output; -A 5: after, the last 5 lines
-B 5: before, the first 5 lines
-C 5: context, 5 lines before and after.

Leave a Comment

Your email address will not be published.