一、正则介绍
正则是学习shell脚本的必学的内容,正则学的好坏直接影响到shell编程能力。
正则表达式:使用单个字符串来描述或匹配一系列符合某个句法规则的字符串。通常用来检索和替换那些符合某个模式的文本内容。正则表达式贯穿到整个运维日常,无论是查找某个文件,还是查询某个日志文件并分析其内容均用到。
正则表达式是一种思想,一种表示方法。常用的工具grep、sed、awk三剑客。
二、grep上
grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户
命令格式:grep -cinvABC ‘关键字‘ 文件名
-c:显示出含关键字有多少行,会显示一个数字
-i:不区分大小写
-n:显示出含关键字的行及行号,前面会标出行号
-v:取反,显示出不含关键字的行
-r:遍历所有子目录
-An:n为数字,表示显示出含关键字的行及下面n行
-Bn;n为数字,表示显示出含关键字要求的行及上面n行
-Cn;n为数字,表示显示出含关键字要求的行,及上下n行
·例子:
[[email protected] ~]# mkdir grep
[[email protected] ~]# cd grep/
[[email protected] grep]# cp /etc/passwd .
[[email protected] grep]# ls
passwd
[[email protected] grep]#
[[email protected] grep]# pwd
/root/grep
[[email protected] grep]# ls
passwd
[[email protected] grep]# grep ‘nologin‘ passwd 显示出passwd下面含有nologin所有行
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
[[email protected] grep]#
[[email protected] grep]# which grep
alias grep=‘grep --color=auto‘ 在Centos7中关键字会有颜色
/usr/bin/grep
[[email protected] grep]#
[[email protected] grep]# grep -c ‘nologin‘ passwd 显示出passwd文件名下含有nologin关键字的行共有15行
15
[[email protected] grep]# grep -n ‘nologin‘ passwd 显示出passwd文件名下显示含有nologin关键字的行及行号
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:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15:dbus:x:81:81:System message bus:/:/sbin/nologin
16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin
17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[[email protected] grep]#
[[email protected] grep]# grep -i ‘nologin‘ passwd 显示出passwd文件名下含有关键字nologin的行,不区分大小写
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
[[email protected] grep]#
[[email protected] grep]# grep -ni ‘nologin‘ passwd 显示出passwd文件名下含有关键字nologin的所在行,不区分大小写
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:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15:dbus:x:81:81:System message bus:/:/sbin/nologin
16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin
17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[[email protected] grep]#
[[email protected] grep]# grep -nv ‘nologin‘ passwd 取反
1:root:x:0:0:root:/root:/bin/bash
2:bin:x:1:1:bin:/bin:/sbin/NOLOGIN 此时还会区分大小写,大写的不是关键字
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:davery:x:1000:1003::/home/davery:/bin/bash
21:uaer1:x:1001:1004::/home/uaer1:/bin/bash
22:user2:x:1002:1005::/home/user2:/bin/bash
23:user3:x:1006:1006::/home/user3:/bin/bash
24:user4:x:1007:898::/home/user4:/bin/bash
25:user6:x:1008:898::/home/user6:/bin/bash
[[email protected] grep]#
[[email protected] grep]# grep -niv ‘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:davery:x:1000:1003::/home/davery:/bin/bash
21:uaer1:x:1001:1004::/home/uaer1:/bin/bash
22:user2:x:1002:1005::/home/user2:/bin/bash
23:user3:x:1006:1006::/home/user3:/bin/bash
24:user4:x:1007:898::/home/user4:/bin/bash
25:user6:x:1008:898::/home/user6:/bin/bash
[[email protected] grep]#
[[email protected] grep]# grep -inv ‘nologin‘ passwd inv不分先后
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:davery:x:1000:1003::/home/davery:/bin/bash
21:uaer1:x:1001:1004::/home/uaer1:/bin/bash
22:user2:x:1002:1005::/home/user2:/bin/bash
23:user3:x:1006:1006::/home/user3:/bin/bash
24:user4:x:1007:898::/home/user4:/bin/bash
25:user6:x:1008:898::/home/user6:/bin/bash
[[email protected] grep]#
[[email protected] grep]# grep ‘root‘ /etc/
grep: /etc/: 是一个目录
[[email protected] grep]#
[[email protected] grep]# grep -r ‘root‘ /etc/ > /tmp/grep.log 针对目录来显示
[[email protected] grep]# grep passwd /tmp/grep.log
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/passwd-:root:x:0:0:root:/root:/bin/bash
/etc/passwd-:operator:x:11:0:operator:/root:/sbin/nologin
/etc/postfix/main.cf:# the system passwd file in the chroot jail is just not practical.
[[email protected] grep]#
[[email protected] grep]# grep -A2 ‘root‘ passwd 在passwd文件名下显示出含有root的行及下面2行
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
[[email protected] grep]#
[[email protected] grep]# grep -nA2 ‘root‘ passwd 在passwd文件名下显示出含有root的行及下面2行,及所在行号
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
--
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
[[email protected] grep]#
[[email protected] grep]# grep -nB2 ‘root‘ passwd 在passwd文件名下显示出含有root的行及上面2行,及所在行号
1:root:x:0:0:root:/root:/bin/bash
--
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] grep]#
[[email protected] grep]# grep -nC2 ‘root‘ passwd 在passwd文件名下显示出含有root的行及上、下面各2行,及所在行号
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
--
8-halt:x:7:0:halt:/sbin:/sbin/halt
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
[[email protected] grep]#
三、grep中正则表达式
[[email protected] grep]# grep ‘[0-9]‘ /etc/inittab 显示出/etc/inittab下所有带数字的行,若行没有数字则显示不出来
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
[[email protected] grep]#
[[email protected] grep]# grep -v ‘[0-9]‘ /etc/inittab 显示出/etc/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:
#
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#
[[email protected] grep]#
[[email protected] grep]# grep -n ‘^#‘ /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:#
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:#
[[email protected] grep]#
[^] 匹配非方括号里面的内容。就是:只要不是方括号里面的内容都显示出来。
^[^] 匹配非方括号里面的内容开头的所有字符,只要不是方括号里面的内容所开头的,都显示出来。
[[email protected] grep]# grep ‘[^0-9]‘ /etc/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
#
[[email protected] grep]# grep ‘^[^0-9]‘ /etc/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
#
[[email protected] grep]#
显示特殊字符相关
[email protected] grep]#
[[email protected] grep]# grep ‘r.o‘ passwd .代表r与o之间任意一个字符,多字符不会标记显示
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] grep]#
[[email protected] grep]# grep ‘ro*o‘ passwd *代表任意字符可以为空
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[[email protected] grep]#
[[email protected] grep]# grep ‘ro*t‘ passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] grep]#
[[email protected] grep]# grep -n ‘:[0-9]‘ 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
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
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
15:dbus:x:81:81:System message bus:/:/sbin/nologin
16:polkitd:x:999:997:User for polkitd:/:/sbin/nologin
17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
18:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
20:davery:x:1000:1003::/home/davery:/bin/bash
21:uaer1:x:1001:1004::/home/uaer1:/bin/bash
22:user2:x:1002:1005::/home/user2:/bin/bash
23:user3:x:1006:1006::/home/user3:/bin/bash
24:user4:x:1007:898::/home/user4:/bin/bash
25:user6:x:1008:898::/home/user6:/bin/bash
[[email protected] grep]#
[[email protected] grep]# grep -n ‘8:*‘ passwd
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
15:dbus:x:81:81:System message bus:/:/sbin/nologin
17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
24:user4:x:1007:898::/home/user4:/bin/bash
25:user6:x:1008:898::/home/user6:/bin/bash
[[email protected] grep]#
[[email protected] grep]# grep -n ‘8:*[8-9]‘ passwd
17:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
24:user4:x:1007:898::/home/user4:/bin/bash
25:user6:x:1008:898::/home/user6:/bin/bash
[[email protected] grep]#
[[email protected] grep]# grep ‘user6.*bash‘ passwd .*表示零个或多个任意字符
user6:x:1008:898::/home/user6:/bin/bash
[[email protected] grep]#
[[email protected] grep]# grep ‘o\{2\}‘ passwd 加入转义符\,o\{2\ }表示括号前边o出现2次重复
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
[[email protected] grep]#
[[email protected] grep]# grep ‘o\{0,3\}‘ passwd 0,3范围匹配,o\{0,3\}表示括号前边出现0次1次2次3次重复
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
davery:x:1000:1003::/home/davery:/bin/bash
uaer1:x:1001:1004::/home/uaer1:/bin/bash
user2:x:1002:1005::/home/user2:/bin/bash
user3:x:1006:1006::/home/user3:/bin/bash
user4:x:1007:898::/home/user4:/bin/bash
user6:x:1008:898::/home/user6:/bin/bash
[[email protected] grep]#
五、egrep表达式
[[email protected] grep]# grep -E ‘o{2}‘ passwd 等同于egrep ‘o{2}‘ passwd
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
[[email protected] grep]#
[[email protected] grep]# egrep ‘o+‘ passwd 表示o出现1次,等同于grep ‘o\{1\}‘ passwd
root:x:0:0:root:/root:/bin/bash
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/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:998:996::/var/lib/chrony:/sbin/nologin
davery:x:1000:1003::/home/davery:/bin/bash
uaer1:x:1001:1004::/home/uaer1:/bin/bash
user2:x:1002:1005::/home/user2:/bin/bash
user3:x:1006:1006::/home/user3:/bin/bash
user4:x:1007:898::/home/user4:/bin/bash
user6:x:1008:898::/home/user6:/bin/bash
[[email protected] grep]# egrep ‘oo+‘ passwd 等同于egrep ‘o{2}‘ passwd
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
[[email protected] grep]#
[[email protected] grep]# egrep ‘o?t‘ passwd 表示o重复1次,后边跟t
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
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[[email protected] grep]# egrep ‘oo?t‘ passwd 表示o重复2次,后边跟t
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] grep]#
[[email protected] grep]# egrep ‘mail|oo‘ passwd 使用管道符| , 显示出mail或者oo字符串
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
[[email protected] grep]#
原文地址:https://www.cnblogs.com/davery/p/8729849.html