9.1 正則介紹_grep上 9.2 grep中 9.3 grep下

第九章 正則

9.1 正則介紹&grep(上)

 

轉:linux

正則

在計算機科學中,是指一個用來描述或者匹配一系列符合某個句法規則的字符串的單個字符串。在不少文本編輯器或其餘工具裏,正則表達式一般被用來檢索和/或替換那些符合某個模式的文本內容。許多程序設計語言都支持利用正則表達式進行字符串操做。正則表達式

正則表達式的基礎  

在最簡單的狀況下,一個正則表達式看上去就是一個普通的查找串。如:正則表達式"test"中沒有包含任何元字符,它能夠匹配"test"和"007test007"等字符串,可是不能匹配"Test"。vim

要想真正的用好正則表達式,須要正確的理解元字符。bash

元字符

. :匹配任何單個字符。例如正則表達式 r.t 匹配這些字符串:rat、rut、r t,可是不匹配root。ssh

$ :匹配行結束符。例如正則表達式 weasel$ 可以匹配字符串"He's a weasel"的末尾,可是不能匹配字符串"They are a bunch of weasels."。編輯器

^ :匹配一行的開始。例如正則表達式 ^When in 可以匹配字符串"When in the course of human events"的開始,可是不能匹配"What and When in the"。工具

*:匹配0個或多個正好在它以前的那個字符。如,正則表達式 .* 意味着可以匹配任意數量的任何字符(表明全部內容)。post

" \ ":脫義符,使這裏列出的這些元字符以普通的字符的身份來進行匹配。如,\ . 是匹配點字符,而不是以通配符的身份出現。spa

[ ]:匹配括號中的任何一個字符。如,正則表達式 r[aeo]d 匹配rad,red,rod。也能夠指定區間 [0-9a-zA-Z] ,即表明全部非特殊字符!命令行

\ <,\ >:匹配詞(word)的開始(\ <)和結束(\ >)。例如正則表達式 \ < the 可以匹配字符串"for the wise"中的"the",可是不能匹配字符串"otherwise"中的"the"。 注意: 這個元字符不是全部的軟件都支持的。

|:將兩個匹配條件進行邏輯「或」(Or)運算。 注意: 這個元字符不是全部的軟件都支持的。

+:匹配1或多個正好在它以前的那個字符。注意: 這個元字符不是全部的軟件都支持的。

?:匹配0或1個正好在它以前的那個字符。注意: 這個元字符不是全部的軟件都支持的。

\ {i\ }\ {i,j\ }: 匹配指定數目的字符,這些字符是在它以前的表達式定義的。例如正則表達式A[0-9]\ {3\ } 可以匹配字符"A"後面跟着正好3個數字字符的串,例如A12三、A348等,可是不匹配A1234。而正則表達式[0-9]\ {4,6\ } 匹配連續的任意4個、5個或者6個數字字符。注意: 這個元字符不是全部的軟件都支持的。

正則表達式的語法支持狀況


注: x表示支持。

轉:

grep(上)

grep (縮寫來自Globally search a Regular Expression and Print)是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹配的行打印出來。Unix的grep家族包括grep、egrep和fgrep,egrep和fgrep的命令都是grep的擴展,支持更多的re元字符,fgrep就是fixed grep或fast grep,它們把全部的字母都看做單詞,也就是說,正則表達式中的元字符表示回其自身的字面意義,再也不特殊。linux使用GNU版本的grep。它功能更強,能夠經過-G、-E、-F命令行選項來使用egrep和fgrep的功能。

語法: grep [options] ‘Word’ filename

grep [-cinvABC] 'word' filename

• -c 行數

• -i 不區分大小寫

• -n 顯示行號

• -v 取反

• -r 遍歷全部子目錄

• -A 後面跟數字,過濾出符合要求的行以及下面n行

• -B 同上,過濾出符合要求的行以及上面n行

• -C 同上,同時過濾出符合要求的行以及上下各n行
 

過濾一個字符串

[root@cham2 grep]# grep 'nologin' passwd
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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
[root@cham2 grep]# grep -c 'nologin' passwd

在Linux7中,grep默認帶color選項:

grep -c  行數

[root@cham2 grep]# grep -c 'nologin' passwd
16
[root@cham2 grep]# cat !$
cat 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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin

grep -n 顯示行號 

[root@cham2 grep]# grep -n 'nologin' passwd
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
11:games:x:12:100:games:/usr/games:/sbin/nologin
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13:nobody:x:99:99:Nobody:/:/sbin/nologin
14:systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
15:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
16:dbus:x:81:81:System message bus:/:/sbin/nologin
17:polkitd:x:998:996:User for polkitd:/:/sbin/nologin
18:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
19:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
20:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
21:chrony:x:997:995::/var/lib/chrony:/sbin/nologin

 grep -i 不區分大小寫

[root@cham2 grep]# cat 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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin

[root@cham2 grep]# grep 'nologin' passwd
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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin

[root@cham2 grep]# grep -i 'nologin' passwd
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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin

[root@cham2 grep]# grep -ic 'nologin' passwd
17

 grep -v 取反

[root@cham2 grep]# grep -v 'nologin' passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/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

grep -r/R•遍歷全部子目錄

[root@cham2 grep]# tree
.
├── grep11
│   └── passwd
└── passwd

1 directory, 2 files
[root@cham2 grep]# grep -R 'root' .
./passwd:root:x:0:0:root:/root:/bin/bash
./passwd:operator:x:11:0:operator:/root:/sbin/nologin
./grep11/passwd:root:x:0:0:root:/root:/bin/bash
./grep11/passwd:operator:x:11:0:operator:/root:/sbin/nologin
[root@cham2 grep]# grep -r 'root' .
./passwd:root:x:0:0:root:/root:/bin/bash
./passwd:operator:x:11:0:operator:/root:/sbin/nologin
./grep11/passwd:root:x:0:0:root:/root:/bin/bash
./grep11/passwd:operator:x:11:0:operator:/root:/sbin/nologin

grep -A 後面跟數字,過濾出符合要求的行以及下面n行

grep -B 同上,過濾出符合要求的行以及上面n行

grep -C 同上,同時過濾出符合要求的行以及上下各n行

[root@cham2 grep]# grep -nA2 'daemon' passwd
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4-adm:x:3:4:adm:/var/adm:/sbin/nologin
5-lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
--
18:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
19-postfix:x:89:89::/var/spool/postfix:/sbin/nologin
20-sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@cham2 grep]# grep -nB2 'daemon' passwd
1-root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/NOLOGIN
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
--
16-dbus:x:81:81:System message bus:/:/sbin/nologin
17-polkitd:x:998:996:User for polkitd:/:/sbin/nologin
18:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
[root@cham2 grep]# grep -nC2 'daemon' passwd
1-root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/NOLOGIN
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4-adm:x:3:4:adm:/var/adm:/sbin/nologin
5-lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
--
16-dbus:x:81:81:System message bus:/:/sbin/nologin
17-polkitd:x:998:996:User for polkitd:/:/sbin/nologin
18:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
19-postfix:x:89:89::/var/spool/postfix:/sbin/nologin
20-sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

9.2 grep(中)

grep應用

grep -n '[0-9]' passwd 它表示方括號裏面的任意一個字符

[root@cham2 grep]# grep '[0-9]' 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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin

grep -vn '[0-9]' /etc/inittab

[root@cham2 grep]# grep -vn '[0-9]' /etc/inittab
1:# inittab is no longer used when using systemd.
2:#
3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
4:#
5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
6:#
7:# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
8:#
11:#
12:# To view current default target, run:
13:# systemctl get-default
14:#
15:# To set a default target, run:
16:# systemctl set-default TARGET.target
17:#

grep -vn ' ^# ' 過濾以#開頭的行(^在此表示以某字符開頭)-vn把不以#好開頭的文件打印出來

[root@cham2 grep]# grep -n '^#' inittab
1:# inittab is no longer used when using systemd.
2:#
3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
4:#
5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
6:#
7:# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
8:#
9:# multi-user.target: analogous to runlevel 3
12:# graphical.target: analogous to runlevel 5
13:#
15:# To view current default target, run:
16:# systemctl get-default
17:#
18:# To set a default target, run:
19:# systemctl set-default TARGET.target
20:#
[root@cham2 grep]# grep -vn '^#' inittab
10:
11:21weqweqweqweqwewqeqwesafdsf
14:1231212412414124124124

#vim inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
!@@%#@%#@^$^&$#&#&#%&#%^$#@^$
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#$%$^%$^$^$^#%#@$@%#^#^#&
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3

21weqweqweqweqwewqeqwesafdsf
# graphical.target: analogous to runlevel 5
#
1231212412414124124124
# To view current default target, run:
# systemctl get-default
1111111111111111111111
#
# To set a default target, run:
222222222222222222222222222
# systemctl set-default TARGET.target
#

[root@cham2 grep]# grep -v '^#' inittab
!@@%#@%#@^$^&$#&#&#%&#%^$#@^$

21weqweqweqweqwewqeqwesafdsf
1231212412414124124124
1111111111111111111111
222222222222222222222222222

grep '[^0-9]' 表示非0-9中任意字符所在的行,即任意非數字所在的行(只要包含非數字字符都算在內,方括號內的‘^’表示否認)

[root@cham2 grep]# grep -n '[^0-9]' inittab 
1:# inittab is no longer used when using systemd.
2:#
3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
4:!@@%#@%#@^$^&$#&#&#%&#%^$#@^$
5:#
6:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
7:#$%$^%$^$^$^#%#@$@%#^#^#&
8:#
9:# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
10:1111111111a
11:#
12:# multi-user.target: analogous to runlevel 3
14:21weqweqweqweqwewqeqwesafdsf
15:# graphical.target: analogous to runlevel 5
16:#
18:# To view current default target, run:
19:# systemctl get-default
21:#
22:# To set a default target, run:
24:# systemctl set-default TARGET.target
25:#

[root@cham2 grep]# grep -n '^[^0-9]' inittab     
1:# inittab is no longer used when using systemd.
2:#
3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
4:!@@%#@%#@^$^&$#&#&#%&#%^$#@^$
5:#
6:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
7:#$%$^%$^$^$^#%#@$@%#^#^#&
8:#
9:# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
11:#
12:# multi-user.target: analogous to runlevel 3
15:# graphical.target: analogous to runlevel 5
16:#
18:# To view current default target, run:
19:# systemctl get-default
21:#
22:# To set a default target, run:
24:# systemctl set-default TARGET.target
25:#

[root@cham2 grep]# grep -nv '^[^0-9]' inittab 
10:1111111111a
13:
14:21weqweqweqweqwewqeqwesafdsf
17:1231212412414124124124
20:1111111111111111111111
23:222222222222222222222222222

grep 'r.o' 在此 「 . 」 表示一個任意字符

[root@cham2 grep]# grep 'r.o' passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@cham2 grep]# vi passwd
[root@cham2 grep]# vi passwd
[root@cham2 grep]# grep 'r.o' passwd
root:x:0:0:root:/root:/bin/bash
dfsdfdsfsdfs:1241124:jksdfhdskjf:sfdsf:rao:325345345
222222:r<o:dfdsfdsg
operator:x:11:0:operator:/root:/sbin/nologin
[root@cham2 grep]# grep 'r.o' passwd
root:x:0:0:root:/root:/bin/bash
dfsdfdsfsdfs:1241124:jksdfhdskjf:sfdsf:rao:325345345
222222:r.o:dfdsfdsg
operator:x:11:0:operator:/root:/sbin/nologin

grep 'o*o' 在此‘ * ’表示其左邊的字符重複n次(n≥0

[root@cham2 grep]# grep 'o*o' passwd
root:x:0:0:root:/root:/bin/bash
dfsdfdsfsdfs:1241124:jksdfhdskjf:sfdsf:rao:325345345
222222:r.o:dfdsfdsg
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spoool/lpd:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
mail:x:8:12:mail:/var/spoooooool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin

grep '.*' 在此' . * '組合表示全部任意字符

匹配單行

grep 'o\ {2\ }' 在此{}表示前面字符或字符串出現的次數,又或者叫重複範圍。(也能夠是一個區間,如'o\ {0,3\ }'表示0個到3個o)不想脫義可以使用-E 或者egrep

[root@cham2 grep]# grep 'o\{2\}' passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spoool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spoooooool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

egrep  'o+o' 在此‘+’表示其左邊的字符重複n此(一次或者屢次)(n≥1,注意和*的區別)

不想脫義可以使用-E 或者egrep

[root@cham2 grep]# grep 'o\+o' passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spoool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spoooooool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

[root@cham2 grep]# grep 'o\+r' passwd
operator:x:11:0:operator:/root:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin

egrep -n 'o?t' 在此?表示其前面的字符出現0或1,要麼有要麼沒有

[root@cham2 grep]# egrep 'o?t' passwd
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halo1t:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

egrep -n 'root|nologin' 在此‘|’表示或者

[root@cham2 grep]# grep -E 'root|nologin' passwd 
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
ad11om:x:3:4:ad1om:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spoool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spoooooool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin

grep -nE '(oo){2}' 在此()內的內容表示一個字符組合,即連續出現兩次‘oo’=‘oooo’字符串

相關文章
相關標籤/搜索