Regular expressions and grep command

1. Concept:

  • Regular expressions are used to find, replace, and delete one or more lines of text strings through the arrangement of some special characters.

  • In the Shell command line, users can use the grep command to test

    Parameters Description
    -b For each line of output, show the byte offset in the file of the line containing the matching string
    -c Show only the found lines Number
    -i Displays the found rows, comparison is not case-sensitive
    -h Instructs grep not to prepend file names to the output when searching for multiple files
    -l Display the file name where the first matching string is located and separate it with newline characters. When a matching string appears multiple times in a file, the file name will not be displayed repeatedly
    -n Display the found line and the line number ( The first line of the file is numbered 1)
    -o Only display matching content
    -A If the match is successful, print the matching line and the following n lines together
    -B If the match is successful, print the matching line and its first n lines together
    -C If the match is successful, print out The matching line and the n lines before and after it are printed together
    -E Equal to egrep, extended
    –color Highlight color to display matched strings
    -v Show lines with no matching string
    -w Matching words
    -x Only display lines that strictly match the entire line
    -P Match the specified strings between Character
    • grep: supports the use of basic regular expressions (note: grep needs to escape {}, {}, egrep does not need to be escaped)

      grep ^SELINUX /etc/selinux/config # Find row data starting with SELINUX
    • egrep: supports the use of extended regular expressions

    • fgrep: Using regular expressions is not supported

2.Basic regular expressions:

  • Basic Regular Expression (BRE), also known as standard regular expression

  • character:

    td>

    Character Description Character Description
    ^word Match at the beginning of each line [str] Match any single character in str
    word$ match at the end of each line [^str] match any Match a single character not in str
    . Match any single character [a-b] Match any character between a and b
    * Match the previous item 0 or more times \ Ignore the special meaning of the following character
    \b Any character after it Must appear as the beginning or end of a word {n,m} match n to m times, the previous character
    . * Match all characters, 0 to many times {n,} At least N times, no limit to more
    ^.* Start with any number of characters {n} n times
    ^$ Indicates a blank line, not a space {,m} Up to m times, No limit to what is missing
    \ Any character following it must appear as the beginning of the word word> Any character preceding it must appear as the end of a word
    \w Match any word character including an underscore
    \W Matches any non-word character. Equivalent to “A-Za-z0-9_”
    \d Matches a numeric character
    \D Matches a non-numeric character. Equivalent to 0-9.
    \s White space
    \S Non-whitespace characters
    [root@quruixiang shellstudy]# echo "1234578125" |grep '5$'
    1234578125
    [root@quruixiang shellstudy]# echo "1234578125" |grep '12.'
    1234578125
    [root@quruixiang shellstudy]# echo "1234578125" |grep '1*'
    1234578125
    [root@quruixiang shellstudy]# echo "123asdfegv123" |grep [ab]
    123asdfegv123
    [root@quruixiang shellstudy]# echo "123asdfegv123" |grep [a-zA-Z]
    123asdfegv123
    [root@quruixiang shellstudy]# echo "3.1415926" |grep '1\{1,\}' # The grep command needs to be escaped when using {}
    3.1415926
    [root@quruixiang shellstudy]# echo "abcdefg" | grep '\babc'
    abcdefg
    [root@quruixiang shellstudy]# grep "bash\>" /etc/passwd
    # 5. Find /etc/inittab containing "starting with s, and words ending in d" pattern lines;
    [root@quruixiang shellstudy]# grep "\<s[a-z]*d\>" /etc/inittab
  • Regular expression character set: (grep + parameter E)

    Character Description
    [[:alnum:]] Matches any letter or number, equivalent to [A-Za-z0-9]
    [[:alpha:]] matches any letter, equivalent to [A-Za-z]
    [[:digit:]] matches any number , equivalent to 0-9
    [[:lower:]] matches any lowercase letter, equivalent to a-z
    [[:upper:]] matches any uppercase letter, equivalent to A-Z
    [[: space:]] Matches any whitespace character, including spaces, tabs, newlines, and page breaks
    [[:blank:]] Match spaces and tabs
    [[:graph:]] Match any A visible printable character, excluding whitespace characters
    [[:print:]] Matches any printable character, including White space characters, but does not include control characters, string terminator ‘\0’, EOF file end character (-1)
    [[:cntrl:]] matches any control character, that is, the first 32 characters in the ASCII character set. For example, newline characters, tab characters, etc.
    [[:punct:]] Matches any punctuation mark, such as “[]”, “{ }” or “,” etc.
    [[:xdigit:]] Matches hexadecimal digits, that is, 0-9, a-f and A-F

3. Extended regular expression:

  • Extended Regular Expression (ERE) supports more metacharacters than basic regular expressions, but extended regular expressions do not support some metacharacters supported by basic regular expressions.

  • character:

    td>

    Character Description Character Description
    + Match the previous item one or more times {j} Match the previous item j repeated matches
    ? Match the previous item 0 or 1 times {j,} Match the previous item j or more times
    (?=exp) Assert that the position after which it appears can match the expression exp {,k} Match the previous item at most k times
    (s|t) Match one of the s items or t items
    ( ) Consider the content in brackets as a whole (word).\1 Define the start and end position of the subexpression (the beginning and end of the string)
    [root@quruixiang shellstudy]# grep -E "^(s|S)" /proc/meminfo
    # 8. Display related information of root, centos or spark users on the current system information;
    [root@quruixiang shellstudy]# grep -E -w "^(root|centos|spark)" /etc/passwd # -w matches words
    # Match databases that do not have Database or information_schema in the database list
    mysql -h127.0.0.1 -uroot -p110119 -e "show databases;" | grep -Ev "Database|information_schema"
    # 4. Find /etc/rc.d/rc .local contains strings of words "starting with to and ending with to";
    [root@quruixiang shellstudy]# grep -E -w "(to).*\1" /etc/rc.d/rc.local # \1 indicates a reference to to
    [root @quruixiang ~]# echo "window10" | grep -P "window(?=10)"
    window10

4. Exercise:

# 1. Display the lines ending with bash in the /etc/passwd file;
[root@quruixiang shellstudy]# grep "bash\>" /etc/passwd
?
# 2. Find the three or four digits in the /etc/passwd file;
[root@quruixiang shellstudy]# grep "[0-9]\{3,4\}" /etc/passwd
?
# 3. Find the lines in the /etc/grub2.cfg file that start with at least one blank character and are followed by non-blank characters;
[root@quruixiang shellstudy]# grep -E "^[[:space:]].*" /etc/grub2.cfg
?
# 4. Find the lines ending with 'LISTEN' followed by 0 or more blank characters in the results of the "netstat -tan" command;;
[root@quruixiang shellstudy]# netstat -tan | grep -E "LISTEN[[:space:]]*\>"
?
# 5. Find the lines that include /dev/ followed by sd or hd and a letter in the results of the "fdisk -l" command;
[root@quruixiang shellstudy]# fdisk -l | grep "/dev/[sh]d[a-z]"
?
# 6. Find the file path in the result of the "ldd /usr/bin/cat" command;
[root@quruixiang shellstudy]# ldd /usr/bin/cat | grep -o "/[^[:space:]]\{1,\}" # -o only displays matching content
?
# 7. Find all lines starting with uppercase or lowercase s in the /proc/meminfo file; use at least three ways to achieve this;
[root@quruixiang shellstudy]# grep "^[Ss]" /proc/meminfo
[root@quruixiang shellstudy]# grep -i "^s" /proc/meminfo # -i ignore case
[root@quruixiang shellstudy]# grep -E "^(s|S)" /proc/meminfo
?
# 8. Display relevant information of root, centos or spark users on the current system;
[root@quruixiang shellstudy]# grep -E -w "^(root|centos|spark)" /etc/passwd # -w matches words
?
# 9. echo outputs an absolute path, and uses egrep to extract its base name;
[root@quruixiang shellstudy]# echo /usr/shellstudy/ | grep -E -o "[^/] + /?$" | cut -d "/" -f 1
# 10. Find the integer between 1-255 in the ifconfig command result;
[root@quruixiang shellstudy]# ifconfig | grep -E -o "([1-9]|[1-9][1-9]|1[0-9][0-9]|2[0- 5][0-5])"
?
# 11. Find the user whose user name is the same as the shell name in the system.
[root@localhost ~]# grep "^\(.*\):.*\1$" passwd
# 1. Display lines starting with size-insensitive h in the /etc/rc.d/rc.sysinit file;
[root@quruixiang shellstudy]# grep -i h /etc/rc.d/rc.sysinit
?
# 2. Display the lines ending with sh in /etc/passwd;
[root@quruixiang shellstudy]# grep "sh$" /etc/passwd
?
# 3. Display lines in /etc/fstab that begin with #, followed by one or more blank characters, and then followed by any non-blank characters;
[root@quruixiang shellstudy]# grep "^#[[:space:]]\{1,\}[^[:space:]]\{1,\}" /etc/fstab
?
# 4. Search for strings containing "starting with to and ending with to" in /etc/rc.d/rc.local;
[root@quruixiang shellstudy]# grep -E -w "(to).*\1" /etc/rc.d/rc.local
?
# 5. Find lines in /etc/inittab that contain the pattern "words starting with s and ending with d";
[root@quruixiang shellstudy]# grep "\<s[a-z]*d\>" /etc/inittab
?
# 6. Find the integer between 1-255 in the ifconfig command result;
[root@quruixiang shellstudy]# ifconfig | grep -E -o "([1-9]|[1-9][1-9]|1[0-9][0-9]|2[0- 5][0-5])"
?
# 7. Display the lines containing "Failed" or "FAILED" in the /var/log/secure file;
[root@quruixiang shellstudy]# grep -E -w "(failed|FAILED)" /var/log/secure
?
# 8. Take out the line in /etc/passwd where the default shell is bash;
[root@quruixiang shellstudy]# grep "bash$" /etc/passwd
?
# 9. List the file information starting with ns and ending with .conf in the /etc/ directory in long format;
[root@quruixiang shellstudy]# ls /etc | grep "ns[a-zA-Z0-9_]\{1,\}.conf$"
# 10. Highlight the colon and the characters on both sides of it in the passwd file;
[root@quruixiang shellstudy]# grep --color ".:." /etc/passwd