[Familiar with Linux] grep \ egrep \ fgrep and regular expression of the text processing tool, egrepfgrep

Source: Internet
Author: User
Tags expression engine egrep

[Familiar with Linux] grep \ egrep \ fgrep and regular expression of the text processing tool, egrepfgrep

It is often said that there are three swordsmen for text processing on Linux, grep, sed, and awk. This article gives a detailed description of grep and introduces a regular expression.

 

Grep
NAME: Row that matches the print mode SYNOPISIS: grep [OPTIONS] PATTERN [FILE...] grep [OPTIONS] [-e PATTERN |-f FILE] [FILE...] common options: -- color = auto: highlight the matched text after coloring. By default, the image is alias grep = 'grep -- color = auto'-I: ignore the case-o of characters: only show the matched strings themselves-v: display the rows that cannot be matched by the pattern-E: supports the use of extended regular expressions-q: silent mode, that is, no information is output.-A #: displays the row to be matched by the Pattern and Its back # Row-B #: displays the row to be matched by the Pattern and Its front # Row-C #: show the rows matched by the pattern and their front and back # Rows

 

Example 1: Match rows with frank under/etc/passwd

[root@localhost tmp]# grep "frank" /etc/passwdfrank:x:1000:1000:frank:/home/frank:/bin/bash

 

Example 2: Match rows with frank in/etc/passwd, case-insensitive

[root@localhost tmp]# grep -i "frank" /etc/passwdfrank:x:1000:1000:frank:/home/frank:/bin/bashFrank:x:1001:1001::/home/Frank:/bin/bash

 

Example 3: Match rows that cannot be matched by bash in/etc/passwd

[root@localhost tmp]# grep -v "bash" /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/shutdown......pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologingdm:x:42:42::/var/lib/gdm:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologintcpdump:x:72:72::/:/sbin/nologin

 

Example 4: only match frank under/etc/passwd, case-insensitive

[root@localhost tmp]# grep -oi "frank" /etc/passwdfrankfrankfrankFrankFrank

 

Example 5: silent mode matches rows containing frank

[root@localhost tmp]# grep -q "frank" /etc/passwd[root@localhost tmp]# 

 

Example 6: match the rows containing ftp and the last three rows under/etc/passwd

[root@localhost tmp]# grep -A 3 "ftp" /etc/passwdftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologinsystemd-bus-proxy:x:999:998:systemd Bus Proxy:/:/sbin/nologinsystemd-network:x:192:192:systemd Network Management:/:/sbin/nologin

 

Example 7: match the rows containing ftp and the first three rows in/etc/passwd

[root@localhost tmp]# grep -B 3 "ftp" /etc/passwdmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

 

Example 8: match the rows containing ftp in/etc/passwd and the two rows before and after ftp.

[root@localhost tmp]# grep -C 2 "ftp" /etc/passwdoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologinsystemd-bus-proxy:x:999:998:systemd Bus Proxy:/:/sbin/nologin

 

Egrep

Supports extended regular expressions to implement text filtering similar to grep, equivalent to grep-E

NAME: print the row that matches the PATTERN. SYNOPISIS: egrep [OPTIONS] PATTERN [FILE...] -I: Ignore the case-o of characters: only display the matched string itself-v: display the row-q: silent mode that cannot be matched by the pattern, that is, no information is output.-A #: displays the row to be matched by the Pattern and Its back # Row-B #: displays the row to be matched by the Pattern and Its front # Row-C #: show the rows matched by the pattern and their front and back # Rows-G: Basic regular expressions are supported.

 

Fgrep

Fgrep is used to search strings instead of matching expression modes. Therefore, regular expressions are supported. If you do not need to use metacharacters to write the expressions, fgrep is better and faster.

Supports options such as-I,-v,-o,-A,-B,-C, and-p.

 

Regular Expression

Regular Expression, a Regular Expression that is written by a special character or text character. Some of them do not represent the literal meaning, but are user-controlled or wildcard functions, it can be divided into basic regular expressions and extended regular expressions.

Basic Regular Expression metacharacters:

Character matching

.: Match any single character; []: match any single character in the specified range; special match: [: digit:] match any single number [: lower:] match any single lowercase letter [: upper:] match any single uppercase letter [: alpha:] match any single letter [: alnum:] match any single letter or number [: punct:] match any single symbol [: space:] match a single space [^]: match any single character out of the specified range;

 

Matching times

It is used after the characters whose occurrences are to be specified and used to limit the occurrences of the first character. The default mode is greedy.

*: Match the first character any number of times: 0, 1, multiple times. *: match any character of any length \? : Match the first character 0 or 1 time; \ +: match the first character 1 or multiple times; \ {m \}: match the first character m times \ {m, n \}: match the first character at least m times, up to n times \ {m, \}: At least m times

 

Position anchoring

^: Specifies the beginning of a line. It is used for the leftmost part of the mode. $: Specifies the end of a line. It is used for the rightmost part of the mode.

Word: a continuous character (string) consisting of non-special characters is called a word.

\ <Or \ B: the beginning of the word, used for the left side of the word mode \> or \ B: The end of the word, the right side of the user word mode \ <PATTERN \>: match the complete word

 

Group and reference

GROUP: \ (\): bind one or more characters together as a whole for processing and then reference: reference the content that matches the pattern in the character grouping bracket that matches the pattern in the preceding grouping brackets or is automatically recorded by the Regular Expression Engine in the internal variable: \ 1: the Mode starts from the left, the first left brace and the matching right brace. The mode matches the content \ 2: The mode starts from the left, the second left brace and the matching right brace are as follows:

 

Example:

1. Display rows not ending with/bin/bash in the/etc/passwd file

[root@localhost tmp]# grep -v "/bin/bash$" /etc/passwdbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologin......gdm:x:42:42::/var/lib/gdm:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologintcpdump:x:72:72::/:/sbin/nologin

2. Find two or three numbers in the/etc/passwd file.

[root@localhost tmp]# grep "\<[[:digit:]]\{2,3\}\>" /etc/passwdmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologin......sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologintcpdump:x:72:72::/:/sbin/nologin

3. Find the rows that start with at least one blank character and are not followed by blank characters in the etc/grub2.cfg file;

[root@localhost tmp]# grep "^[[:space:]]\+[^[:space:]]" /etc/grub2.cfg   load_env   set default="${next_entry}"   set next_entry=   save_env next_entry   set boot_once=true   set default="${saved_entry}"  menuentry_id_option="--id"  menuentry_id_option=""  set saved_entry="${prev_saved_entry}"  save_env saved_entry  set prev_saved_entry=  save_env prev_saved_entry  set boot_once=true

4. Find the line ending with LISTEN followed by 0, 1 or multiple spaces in the result of the "netstat-tan" command

[root@localhost tmp]# netstat -tan | grep  "LISTEN[[:space:]]*$"tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     tcp6       0      0 :::111                  :::*                    LISTEN     tcp6       0      0 :::22                   :::*                    LISTEN     tcp6       0      0 ::1:631                 :::*                    LISTEN     tcp6       0      0 ::1:25                  :::*                    LISTEN 

 

Extended Regular Expression metacharacters:

Character matching

.: Match any single character; []: match any single character in the specified range; special match: [: digit:] match any single number [: lower:] match any single lowercase letter [: upper:] match any single uppercase letter [: alpha:] match any single letter [: alnum:] match any single letter or number [: punct:] match any single symbol [: space:] match a single space [^]: match any single character out of the specified range;

 

Matching times

It is used after the characters whose occurrences are to be specified and used to limit the occurrences of the first character. By default, it works in greedy mode.

*: Match the first character any number of times: 0, 1, multiple times. *: match any character of any length? : Match the first character 0 times or 1 time; +: match the first character 1 time or multiple times; {m}: match the previous character m times {m, n}: match the first character at least m times, at most n {m,}: At least m times

 

Position anchoring

^: Specifies the beginning of a line. It is used for the leftmost part of the mode. $: Specifies the end of a line. It is used for the rightmost part of the mode.

Word: a continuous character (string) consisting of non-special characters is called a word.

\ <Or \ B: the beginning of the word, used for the left side of the word mode \> or \ B: The end of the word, the right side of the user word mode \ <PATTERN \>: match the complete word

 

Group and reference

GROUP: () binds one or more characters as a whole for processing and then references: reference the content that matches the pattern in the character grouping bracket that matches the pattern in the preceding grouping brackets or is automatically recorded by the Regular Expression Engine in the internal variable: \ 1: the Mode starts from the left, the first left brace and the matching right brace. The mode matches the content \ 2: The mode starts from the left, the second left brace and the matching right brace are as follows:

 

Or

A | B: a or bC | cat: C or cat
(C | c) at: cat or Cat

 

Small exercises

1. List the usernames of all logged-on users on the current system. Note: Only one user is displayed when you log on to the same system multiple times.

Who | grep-o "^ \ <[: alpha:] *" | uniqView Code

2. Retrieve Information about the user logging on to the current system.

Id 'who | tail-1 | grep-o "^ \ <[[: alpha:] *"'View Code

3. Retrieve the shell on the current system that is most used as the default shell.

Cut-d:-f7/etc/passwd | uniq-c | sort-n | tail-1 | cut-d ''-f7View Code

4. Set the maximum information of the last 10 users in the Third Field in/etc/passd to uppercase and save it to the/tmp/maxuser.txt file.

[Root @ localhost ~] # Sort-t:-k3-n/etc/passwd | tail-10 | tr 'a-z'' a-Z' &>/tmp/maxuser.txtView Code

5. Retrieve the IP address of the current host

Ifconfig | grep-Eo "([1-9] | [1-9] [1-9] | 1 [0-9] [0-9] | 2 [0-9- 5] [0-4]) \. ([0-9] | [1-9] [1-9] | 1 [0-9] [0-9] | 2 [0-5] [0-5 ]) \. ([0-9] | [1-9] [1-9] | 1 [0-9] [0-9] | 2 [0-5] [0-5 ]) \. ([1-9] | [1-9] [1-9] | 1 [0-9] [0-9] | 2 [0-5] [0-4 ]) [[: space:] "| grep-v" 127.0.0.1 "or ifconfig | grep" [[: space:] *] \ <inet \> "| cut-d''-f10 | grep-v "127.0.0.1"View Code

6. List the names of all files ended with. conf In The/etc directory, convert the names to uppercase, and save them to the/tmp/etc. conf file.

Find/etc-name "*. conf "| egrep-o" [^/] [^/] * $ "| tr 'a-z'' a-Z'>/tmp/etc. testView Code

7. display the total number of sub-directories or files under the/var directory

Ls/etc/| wc-lView Code

8. Retrieve the names of the 10 groups with the smallest value in the Third Field of/etc/group.

Sort-t:-k3-n/etc/group | head-10 | cut-d:-f1View Code

9. Merge the/etc/fstab and/etc/issue files into the same content and save it to the/tmp/etc. test file.

Cat/etc/issue/etc/fstab | tee/tmp/etc. test &>/dev/nullView Code

10. display the rows starting with uppercase or lowercase S in the/proc/meminfo file.

[Root @ localhost home] # egrep "^ [sS]"/proc/meminfo [root @ localhost home] # egrep-I "^ s"/proc/meminfoView Code

11. The default shell in the/etc/passwd file is displayed as a non-/sbin/nologin user.

[Root @ localhost home] # grep-v "/sbin/nologin"/etc/passwdView Code

12. display users whose default shell is/bin/bash in the/etc/passwd file

[Root @ localhost home] # grep "/bin/bash"/etc/passwdView Code

13. Find one or two digits in the/etc/passwd file.

[Root @ localhost/] # egrep "\ <[[: digit:] {1, 2} \>"/etc/passwdView Code

14. display at least one line starting with a blank character in/boot/grub2/grup. cfg

[Root @ localhost/] # egrep "^ [[: space:] + [^ [: space:]"/boot/grub2/grub. cfgView Code

15. The/etc/rc. d/rc. local file starts with # and is followed by at least one blank character and at least one non-blank line.

[Root @ localhost/] # egrep "^ # [[: space:] + [^ [: space:]"/etc/rc. d/rc. local # this file is added for compatibility purposes # It is highly advisable to create own using EMD services or udev rules # to run scripts during boot instead of using this file. # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # Please note that you must run 'chmod + x/etc/rc. d/rc. local' to ensure # that this script will be executed during boot.View Code

16. Execute the netstat-tan command with 'listen' followed by a blank character.

[Root @ localhost/] # netstat-tan | egrep "LISTEN [[: space:] +" tcp 0 0.0.0.0: 111 0.0.0.0: * LISTEN tcp 0 0 0.0.0.0: 22 0.0.0.0: * LISTEN tcp 0 0 127.0.0.1: 631 0.0.0.0: * LISTEN tcp 0 0 127.0.0.1: 25 0.0.0.0: * LISTEN tcp6 0 0: 111 ::: * LISTEN tcp6 0 0: 22: * LISTEN tcp6 0 0: 1: 631: * LISTEN tcp6 0 0 0: 1: 25: * LISTENView Code

17. Add the user bash, testbash, basher, and nologin (the user's shell is/sbin/nologin), and find out the information of the user whose username is the same as the default shell on the current system.

[Root @ localhost/] # useradd bash [root @ localhost/] # useradd testbash [root @ localhost/] # useradd basher [root @ localhost/] # useradd-s/sbin/ nologin [root @ localhost/] # egrep "^ (\ <[a-z] + \> ). * \ 1 $ "/etc/passwdsync: x: 5: 0: sync:/sbin:/bin/syncshutdown: x: 6: 0: shutdown:/sbin: /sbin/shutdownhalt: x: 7: 0: halt:/sbin/haltbash: x: 2004: 2004:/home/bash:/bin/bashnologin: x: 2007: 2007:/home/nologin:/sbin/nologinView Code

 

Related Article

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.