Text filtering tool (grep)

grep command:
grep is a powerful text search tool. It checks the target file line by line based on the user-specified "" and filter conditions written by regular expression metacharacters, prints the matched lines, and prints to the terminal window by default.
Regular expression: A pattern written by a type of special characters and text characters, andDoes not represent the literal meaningExpress control or wildcard function

 用法:
 grep [OPTIONS] PATTERN [FILE...]
 grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
–color=auto Highlight the matched text
-i Ignore character case
-n Show line number
-E Support the use of extended regular expressions
-O Print the matched line
-v Display the lines that cannot be matched
-A# Next few lines
-B # First few lines
-C # A few lines before and after
[root@localhost ~]# grep --color=auto "UUID" /etc/fstab
UUID=05f664d6-53c8-4e53-9291-b1321936d79c /                       ext4    defaults        1 1
UUID=758db803-7a5d-493b-be16-68ca2d050d55 /boot                   ext4    defaults        1 2
UUID=93fc053e-b9a0-4439-a822-3575ee7f3f3d swap                    swap    defaults        0 0

Matches:

* Match the preceding character any number of times. 0 times, 1 time or multiple times
.* Match any character of any length
\? Match the character on the face 0 or 1, at most once
\+ Match the character before it one or more times, at least once
\{m\} Match the preceding character m times
\{m,n\} Match the preceding character at least m times and at most n times
\{0,n\} At most n times
\{m,\} At least m times
实例:
[root@localhost tmp]# cat file
xxxxxy
xyxyxyxyxy
xyyyyxsakfxy
xakfafhlkashfyasbx
asflaklkfhxy
[root@localhost tmp]# grep --color=auto(高量显示,找到的字符用其他颜色标出) x*y file(前面的x出现0次或者多次y出现一次)
xxxxxy
xyxyxyxyxy
xyyyyxsakfxy
xakfafhlkashfyasbx
asflaklkfhxy
[root@localhost tmp]# grep "x\{2,4\}y" file(记得加引号)
xxxxxy

Position anchoring:

^ Anchor at the beginning of the line for the leftmost side of the pattern
$ End-of-line anchoring, used on the rightmost side of the pattern
^ $ blank line
^pattern$ Use pattern to match the entire line
\< Anchor at the beginning of a word, used on the left side of a word
\> Suffix anchoring, used on the right side of words
\<pattern\> Anchored as a pattern
1.显示/etc/passwd文件中不以/bin/bash结尾的行
[root@localhost tmp]# grep  -v '/bin/bash&' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
2.显示/etc/passwd中的两位数或者三位数
[root@localhost tmp]# grep '\<[[:digit:]]\{2,3\}\>' /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
3.找出 netstat -tan 命令结果中以LISTEN后跟一个或者多个空白字符结尾的行
[root@localhost tmp]# netstat -tan | grep "LISTEN[[:space:]]\{1,\}$"
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN
tcp        0      0 :::22                       :::*                        LISTEN
tcp        0      0 ::1:25                      :::*                        LISTEN

Grouping:

\(\) Group, bundle one or more characters together and treat them as a whole

Quote:

\1 Starting from the left of the pattern, the characters matched by the pattern between the first left bracket and the matching right bracket
\2 Starting from the left of the pattern, the characters matched by the pattern between the second left parenthesis and the matching right parenthesis
# Starting from the left of the pattern, the characters matched by the pattern between the #th left bracket and the matching right bracket
实例:
[root@localhost tmp]# cat file
He loves his lover.
He likes his lover.
She likes her liker.
She loves her liker.
[root@localhost tmp]# grep "\(h\).*\(l..e\).*\1.*\2" file
She likes her liker.

To find out the IP address in the ifconfig command?

[root@localhost tmp]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:B6:F8:F4
          inet addr:192.168.176.128  Bcast:192.168.176.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:feb6:f8f4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3493 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1659 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:283560 (276.9 KiB)  TX bytes:183236 (178.9 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
[root@localhost tmp]# ifconfig | grep  "\(192\).*[[:digit:]]$" | cut -d ":" -f 2 | cut -d " " -f 1
192.168.176.128

Guess you like

Origin blog.csdn.net/qq_44944641/article/details/104710642