【CentOS 7基礎36】,正則之grep命令#

shallow丿ovephp


grep

  • grep [-cinvABC] 'word' filename
  • -c 行數
  • -i 不區分大小寫
  • -n 顯示行好
  • -v 取反
  • -r 遞歸遍歷全部子目錄
  • -A 後面跟數字,過濾出符合要求的行以及下面n行
  • -B 同上,過濾出要求的行以上面n行
  • -C 同上,同時過濾出符合要求的行以及上下各n行
[root@localhost ~]# mkdir grep
[root@localhost ~]# cd grep
[root@localhost grep]# cp /etc/passwd .
[root@localhost grep]# ls
	passwd
[root@localhost grep]# pwd
	/root/grep
[root@localhost 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
	dbus:x:81:81:System message bus:/:/sbin/nologin
	polkitd:x:999:998:User for polkitd:/:/sbin/nologin
	avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
	avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
	postfix:x:89:89::/var/spool/postfix:/sbin/nologin
	sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@localhost grep]# which grep
	alias grep='grep --color=auto'
		/usr/bin/grep
[root@localhost grep]# /usr/bin/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
	dbus:x:81:81:System message bus:/:/sbin/nologin
	polkitd:x:999:998:User for polkitd:/:/sbin/nologin
	avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
	avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
	postfix:x:89:89::/var/spool/postfix:/sbin/nologin
	sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

因爲grep命令自帶--color=auto顏色標識,過濾出來自動標明。若使用絕對路徑/usr/bin/grep則沒有顏色標明。mysql

行數-c

[root@localhost grep]# grep -c 'nologin' passwd
	15

顯示行號-n

[root@localhost grep]# grep -n 'nologin' passwd
	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
	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:dbus:x:81:81:System message bus:/:/sbin/nologin
	15:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
	16:avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
	17:avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
	18:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
	19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

不區分大小寫-i

將其中某一行改成大寫sql

[root@localhost grep]# grep -n 'nologin' passwd
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
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:dbus:x:81:81:System message bus:/:/sbin/nologin
15:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
16:avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
17:avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
18:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
21:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
22:php-fpm:x:1001:1001::/home/php-fpm:/sbin/nologin
23:apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

[root@localhost grep]# grep -ni 'nologin' passwd
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
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:dbus:x:81:81:System message bus:/:/sbin/nologin
15:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
16:avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
17:avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
18:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
21:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
22:php-fpm:x:1001:1001::/home/php-fpm:/sbin/nologin
23:apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

能夠發現第9行,以-i過濾是不區分大小寫apache

取反-v

[root@localhost grep]# grep -v 'nologin' passwd
	root:x:0:0:root:/root:/bin/bash
	sync:x:5:0:sync:/sbin:/bin/sync
	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
	halt:x:7:0:halt:/sbin:/sbin/halt
	user1:x:1002:1002::/home/user1:/bin/bash
	user2:x:1003:1003::/home/user2:/bin/bash
[root@localhost grep]# grep -inv 'nologin' passwd
	1:root:x:0:0:root:/root:/bin/bash
	6:sync:x:5:0:sync:/sbin:/bin/sync
	7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
	8:halt:x:7:0:halt:/sbin:/sbin/halt
	20:user1:x:1002:1002::/home/user1:/bin/bash
	21:user2:x:1003:1003::/home/user2:/bin/bash

過濾出來後將剩餘的行顯示出來bash

遞歸遍歷全部子目錄-r

[root@localhost grep]# grep -r 'hello' /root/
	/root/.bash_history:echo helloworld > 1.txt
	/root/.bash_history:echo helloworld > 1.txt
	/root/.bash_history:echo helloworld >> 1.txt
	/root/.bash_history:echo helloworld 2> 1.txt
	/root/.bash_history:hello=world
	/root/.bash_history:echo $hello
	/root/.bash_history:echo $hello
	/root/.bash_history:echo $hello
	/root/.bash_history:echo $hello
	/root/.bash_history:export hello=world
	/root/.bash_history:echo $hello
	/root/.bash_history:echo $hello
	/root/.bash_history:echo $hello
	/root/.bash_history:unset hello
	/root/.bash_history:echo $hello
	/root/.bash_history:vi hello.sh
	/root/.bash_history:chmod + x hello.sh
	/root/.bash_history:chmod +x hello.sh
	/root/.bash_history:sh hello.sh
	/root/.bash_history:echo "helloworld" | tr '[hw]' '[HW]'
	/root/.bash_history:echo "helloworld" | tr 'h' 'H'
	/root/.bash_history:echo "helloworld" | tr '[a-z]' '[A-Z]'
	/root/.bash_history:[ -d hello] || mkdir hello
	/root/.bash_history:[ -d hello] || mkdir hello
	/root/.bash_history:[ -d hello ] || mkdir hello
	/root/.bash_history:[ -d hello ] && mkdir hello
	/root/.bash_history:export hello=world
	/root/.bash_history:echo $hello
	/root/hello.sh:echo "hello!"

過濾出符合要求的行以及下面n行,-A+number

[root@localhost grep]# grep 'root' /etc/passwd
	root:x:0:0:root:/root:/bin/bash
	operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost grep]# grep -A2 'root' /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
	--
	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

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

[root@localhost grep]# grep -B2 'root' /etc/passwd
	root:x:0:0:root:/root:/bin/bash
	--
	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

過濾出符合要求的行以及上下面n行,-C+number

[root@localhost grep]# grep -C2 'root' /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
	--
	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
  • 'string' 單引號 (single quote) 被單引號用括住的內容,將被視爲單一字串。在引號內的表明變數的$符號,沒有做用,也就是說,他被視爲通常符號處理,防止任何變量替換。 heyyou=homeecho '$heyyou' # We get $heyyoussh

  • "string" 雙引號 (double quote) 被雙引號用括住的內容,將被視爲單一字串。它防止通配符擴展,但容許變量擴展。這點與單引數的處理方式不一樣。 heyyou=homeecho "$heyyou" # We get homephp-fpm

  • `command` 倒引號 (backticks) 在前面的單雙引號,括住的是字串,但若是該字串是一列命令列,會怎樣?答案是不會執行。要處理這種狀況,咱們得用倒單引號來作。 fdv=`date +%F`echo "Today $fdv" 在倒引號內的 date +%F 會被視爲指令,執行的結果會帶入 fdv 變數中post

[0-9]

[root@localhost 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
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
php-fpm:x:1001:1001::/home/php-fpm:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@localhost grep]# grep -nv '[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:#

以任意字符開頭^+str

[root@localhost grep]# cp /etc/inittab .
[root@localhost grep]# cat passwd >> inittab
[root@localhost 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
10:# graphical.target: analogous to runlevel 5
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:#
[root@localhost grep]# grep -vn '^#' inittab 
18:root:x:0:0:root:/root:/bin/bash
19:bin:x:1:1:bin:/bin:/sbin/nologin
20:daemon:x:2:2:daemon:/sbin:/sbin/nologin
21:adm:x:3:4:adm:/var/adm:/sbin/nologin
22:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
23:sync:x:5:0:sync:/sbin:/bin/sync
24:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
25:halt:x:7:0:halt:/sbin:/sbin/halt
26:mail:x:8:12:mail:/var/spool/mail:/sbin/NOLogin
27:operator:x:11:0:operator:/root:/sbin/nologin
28:games:x:12:100:games:/usr/games:/sbin/nologin
29:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
30:nobody:x:99:99:Nobody:/:/sbin/nologin
31:dbus:x:81:81:System message bus:/:/sbin/nologin
32:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
33:avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
34:avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
35:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
36:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
37:mysql:x:1000:1000::/home/mysql:/bin/bash
38:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
39:php-fpm:x:1001:1001::/home/php-fpm:/sbin/nologin
40:apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@localhost grep]# grep -vn '^#' inittab | grep -v '^3'
18:root:x:0:0:root:/root:/bin/bash
19:bin:x:1:1:bin:/bin:/sbin/nologin
20:daemon:x:2:2:daemon:/sbin:/sbin/nologin
21:adm:x:3:4:adm:/var/adm:/sbin/nologin
22:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
23:sync:x:5:0:sync:/sbin:/bin/sync
24:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
25:halt:x:7:0:halt:/sbin:/sbin/halt
26:mail:x:8:12:mail:/var/spool/mail:/sbin/NOLogin
27:operator:x:11:0:operator:/root:/sbin/nologin
28:games:x:12:100:games:/usr/games:/sbin/nologin
29:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
40:apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@localhost grep]# grep -vn '^#' inittab | grep -v '^[1-3]'
40:apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

[]非0-9

[root@localhost grep]# grep '[0-9]' inittab 
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
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
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
php-fpm:x:1001:1001::/home/php-fpm:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@localhost grep]# grep '[^0-9]' 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
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#
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
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
php-fpm:x:1001:1001::/home/php-fpm:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

能夠看到非0-9之外的字符都呈紅色code

[root@localhost grep]# grep '^[^0-9]' 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
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#
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
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
php-fpm:x:1001:1001::/home/php-fpm:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

以非0-9的任意字符開頭遞歸

[a-zA-Z]

[root@localhost grep]# grep '^[^a-zA-Z]' 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
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#

.任意一個字符

[root@localhost grep]# grep 'r.o' inittab 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost grep]# grep 'r..t' inittab 
# 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
# graphical.target: analogous to runlevel 5
# To view current default target, run:
# To set a default target, run:
# systemctl set-default TARGET.target
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@localhost grep]# grep '/./' inittab 
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
[root@localhost grep]# grep '/../' inittab 
[root@localhost grep]# grep '/.../' inittab 
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
root:x:0:0:root:/root:/bin/bash
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
mail:x:8:12:mail:/var/spool/mail:/sbin/NOLogin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

*零個或多個

[root@localhost grep]# grep 'ro*' inittab 
# inittab is no longer used when using systemd.
# 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
# graphical.target: analogous to runlevel 5
# To view current default target, run:
# To set a default target, run:
# systemctl set-default TARGET.target
root:x:0:0:root:/root:/bin/bash
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
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@localhost grep]# grep 'roo*' inittab 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

.*任意字符

[root@localhost grep]# grep '.*' 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
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#
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
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
php-fpm:x:1001:1001::/home/php-fpm:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@localhost grep]# grep 'mysql.*bash' inittab 
mysql:x:1000:1000::/home/mysql:/bin/bash
[root@localhost grep]# grep 'mysql.*' inittab 
mysql:x:1000:1000::/home/mysql:/bin/bash

str+{number}匹配str重複的次數

[root@localhost grep]# grep 'o{2}' inittab

[root@localhost grep]# grep 'o\{2\}' inittab 
root:x:0:0:root:/root:/bin/bash
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
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

若重複的次數爲oooo也能匹配出來

str+{number,number}匹配範圍

[root@localhost grep]# grep '9\{1,3\}' inittab 
nobody:x:99:99:Nobody:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin

egrep擴展

[root@localhost grep]# egrep '9{1,3}' inittab 
nobody:x:99:99:Nobody:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin

不須要反斜槓

-E同上

[root@localhost grep]# grep -E '9{1,3}' inittab 
nobody:x:99:99:Nobody:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin

(str){number,number}

[root@localhost grep]# grep -E '(99){1,3}' inittab 
nobody:x:99:99:Nobody:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin

str+匹配一次或屢次str

[root@localhost grep]# grep 'o\+b' inittab 
nobody:x:99:99:Nobody:/:/sbin/nologin
[root@localhost grep]# grep 'o\+t' inittab 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

str?匹配零次或一次str

[root@localhost grep]# 
[root@localhost grep]# grep 'o\?t' inittab 
# inittab is no longer used when using systemd.
# 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
# graphical.target: analogous to runlevel 5
# To view current default target, run:
# systemctl get-default
# To set a default target, run:
# systemctl set-default TARGET.target
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@localhost grep]# grep '70\?:' inittab 
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
[root@localhost grep]# grep '70\?:A' inittab 
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin

str|str或者

[root@localhost grep]# grep 'root\|mysql' inittab 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash

[root@localhost grep]# grep 'root\|mysql' inittab 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
[root@localhost grep]# grep 'root\|mysql\|nobody' inittab 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
相關文章
相關標籤/搜索