Shell script – GREP and regular expressions

grep command

1, grep program: Linux Three Musketeers–grep, awk, sed

2, GrepL: text line filtering tool

< p>  sed: text line editor

  Awk: report generator (formatting text output)

3, grep contains three commands: grep, egrep, fgrep, they are Used for pattern matching

           Egrep=grep -E  //Use extended regular expression to match

                                    Matching

            *grep uses regular expressions for text matching by default

4, grep command usage: grep [option]…..PATTERN [filename]

< p>5. Common parameters of Grep: -E (supports the use of extended regular expressions, regexp (regular expression))

          -P (use the regular expression engine of the per language to search (each The regular expression engines of different languages ​​are different, even the regular expression engine used by sed grep awk is different)))

          -i (ignore case), -v (invert the selection), -n (Display line number), –color (display coloring)

          -o (only output the matched content, the default output is the matched line)

PATTERN—Regular Expression

1. Function: Use some special characters to express the content of a type of character, and then give it to the previous command for execution. If you use the meaning of the special character itself, you need to escape it with \; < /p>

2. Character matching:. (represents any character, equivalent to?), [] (a character in the matching range), [^] (a character outside the range)

     Character class: [:digit:] (number) [:alnum:] (number and uppercase and lowercase letters) [:alpha:] (upper and lowercase letters) [:lower:] (lowercase letters)

[:upper:] (uppercase letter) [:space:] (space) [:punct:] (special symbol)

3. Number of matching: * (matching the preceding character 0 times to n (unnumbered) ) Times)

     ? (Match the preceding character 0 to 1 time)

       + (match the preceding character 1 to n times)

       \{m\} (match the preceding character m Times)

       \{m,n\} (match the preceding character m to n (fixed value) times)

       \{m,\} (match the preceding character at least m times)

4. Position anchoring: ^ (anchoring the beginning of the line)

       $ (anchoring the end of the line)

       \b (anchoring the word Head and anchor ending)

       \>(anchored ending)

       \<(anchored ending)

5. Grouping characteristics: By default , The Linux system will group and specify the variable, the representation of the variable is \1\2\3..

Grouping character: grouping character: \(\) (example: \(abc\)*—- -Match 0 to n times abc)

if statement:

share picture

Under certain conditions, if the conditions are not met, we must manually exit the program, otherwise the following code cannot be executed;

Specify the output code: exit 0, exit 1

When the program error is output, it can be used to judge the program error.

Exit exit code, the following value is You can define it yourself. Generally, the correct value is 0, and the error value is non-zero.

Exercise:

1. Display the line starting with size s in the /proc/meminfo file

1
grep< /code> -i "^s" /proc/meminfo code>

2, display / etc/passwd file without /bin /bash ending line

1
grep - v "/bin/bash$" /etc/passwd

3, display/ The username of the user with the largest UID in the etc/passwd file

1
sort -n -t:- k3 /etc/passwd | tail -1 | cut -d: -f1

4. If the user root exists, display its default shell program

Method 1:

< div id="highlighter_48990" class="syntaxhighlighter bash">

1
grep "^root\>" /etc/passwd &> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7

Method two:

1
id root &> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7

5. Find the two or three digits in /etc/passwd

1
< code class="bash functions">grep -w "[0-9]\{2,3\} " /etc/passwd

6. In the /etc/rc.d/rc.sysinit file, display the lines that start with at least one blank character and cut the following non-blank characters

< td class="gutter">

1
grep "^[[:space:]]\+.*[^[:space:]]$" /etc/rc .d /rc .sysinit

7. Find out the result of the “netstat -tan” command and use “LISTEN” followed by 0, 1 or more blanks

1
netstat -tan | grep "LISTEN[[:space:]]*$"

8. Add users bash, testbash, basher and nologin (the shell is /sbin < img alt="Share a picture" src="/wp-content/uploads/images/os/unix/1626797359015.gif" >login) Then find the line in the /etc/passwd file where the username is the same as the shell name
Share a picture

1
grep -i "^s" /proc/meminfo

1
grep -i "^s" /proc/meminfo

< /div>

1

grep -i "^s" /proc/meminfo

grep -i< /code> "^s" /proc/meminfo

1
grep - v "/bin/bash$" < code class="bash plain">/etc/passwd

1
grep -< /code> v "/bin/bash$" /etc/passwd

1

grep - v "/bin/bash$" /etc/passwd

grep - v "/bin/bash $" /etc/pas swd

1
sort -n -t: -k3 /etc /passwd | tail -1 | cut -d: -f1

< div class="line number1 index0 alt2"> 1
sort -n -t: -k3 /etc/passwd | tail - 1 | cut -d: -f1

1

sort -n -t: -k3 /etc/passwd |< /code> tail -1 | cut -d: -f1

sort - n -t: -k3 /etc/passwd | tail -1 | cut -d: -f1

< td class="code">

grep " ^root\>" /etc/passwd &> /dev/null && grep "^root\> " /etc/passwd | cut -d: -f7

1

1
grep "^root\>" /etc/passwd &> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7

1

grep "^root\>" /etc/passwd &> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7

< code class="bash functions">grep "^root\>" /etc/passwd < code class="bash plain">&> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7

1
id root &> /dev/null && grep "^root\>" /etc/ passwd | cut -d: -f7

< /table>

1

id root &> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7

id root &> /dev/null && grep "^root\>" /etc/passwd | cut -d : -f7

1
id root &> code> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7

< tr>

1
grep -w "[0-9]\{2,3\}" /etc/passwd

< p>

1
grep -w "[0-9]\{2,3\}" /etc/passwd

1

grep -w "[0-9]\{ 2,3\}" /etc/passwd

grep -w "[0-9]\{2,3\}" /etc/passwd

1
< div class="container">

grep "^[[:space:]] \+.*[^[:space:]]$" /etc/rc .d /rc .sysinit

1
grep < code class="bash string">"^[[:space:]]\+.*[^[:space:]]$" /etc/rc .d /rc .sysinit

1

grep "^[[:space:]]\+.*[^[:space:]]$" /etc/rc .d /rc .sysinit

grep "^[[:space: ]]\+.*[^[:space:]]$" /etc/rc .d code> /rc .sysinit

1
netstat -tan | grep "LISTEN[[:space:]]*$"

< tr>

1
netstat - tan | grep "LISTEN[[:space:]]*$"

< /div>

1

netstat -tan | grep "LISTEN [[:space:]]*$"

netstat -tan | grep "LISTEN[[:space:]]*$"

8. Add users bash, testbash, basher and nologin (the shell is /sbin share picturelogin) and then Find the line in the /etc/passwd file where the username is the same as the shell name

share picture