对于一些常用的字符集,系统做了定义:
跟基本正则用法一样
{m,n} :匹配其前面出现的字符至少m次,至多n次。
? :匹配其前面出现的内容0次或1次,等价于{0,1}。
* :匹配其前面出现的内容任意次,等价于{0,},所以 ".*" 表述任意字符任意次,即无论什么内容全部匹配。
+:匹配前面出现的内容1次或1次以上
跟基本正则用法一样
(string) :将string作为一个整体方便后面引用
\1 :引用第1个左括号及其对应的右括号所匹配的内容。
\2 :引用第2个左括号及其对应的右括号所匹配的内容。
\n :引用第n个左括号及其对应的右括号所匹配的内容。
str | num:匹配 str 或 num 内容
[root@maintain data]# grep -i 'failed' /var/log/secure Sep 24 13:25:11 localhost login: pam_systemd(login:session): Failed to release session: Interrupted system call Sep 27 02:58:13 maintain login: FAILED LOGIN 1 FROM tty1 FOR (unknown), User not known to the underlying authentication module Sep 27 06:45:29 maintain sshd[1115]: pam_systemd(sshd:session): Failed to release session: Interrupted system call Sep 27 06:45:32 maintain sshd[1093]: pam_systemd(sshd:session): Failed to release session: Interrupted system call Sep 27 07:03:45 maintain sshd[1138]: pam_systemd(sshd:session): Failed to release session: Interrupted system call Sep 27 07:40:19 maintain sshd[1188]: pam_systemd(sshd:session): Failed to release session: Interrupted system call
[root@maintain data]# egrep -v '^#|^$' /etc/selinux/config SELINUX=disabled SELINUXTYPE=targeted
[root@maintain data]# cat reg.txt Zhang Dandan 41117397 :250:100:175 Zhang Xiaoyu 390320151 :155:90:201 Meng Feixue 80042789 :250:60:50 Wu waiwai 70271111 :250:80:75 Liu Bingbing 41117483 :250:100:175 Wang Xiaoai 3515064655 :50:95:135 Zi Gege 1986787350 :250:168:200 Li Youjiu 918391635 :175:75:300 Lao Nanhai 918691635 :250:100:175 [root@maintain data]# grep -w 'Gege' reg.txt Zi Gege 1986787350 :250:168:200 [root@maintain data]# grep -o 'Gege' reg.txt Gege
grep -v "/bin/bash$" /etc/passwd
grep '[1-9]\{1,2\}[1-9]' /etc/passwd
grep '^[[:space:]]\+[[:alpha:]]' test.txt
netstat -tan | grep 'LISTEN[[:space:]].*$'
[root@maintain data]# cat > test.txt <<EOF > He love his lover > He likes his lover > She likes her liker > She loves her liker > EOF [root@maintain data]# grep '\(l..e\).*\1' test.txt He love his lover She likes her liker
egrep "^(S|s)" /proc/meminfo egrep ^[Ss] /proc/meminfo
egrep ^\(root\|python\|maintain\) /etc/passwd
grep "[[:alpha:]]\+(" /etc/rc.d/init.d/functions egrep '[[:alpha:]]\+|\)|\(' /etc/rc.d/init.d/functions
echo /home/maintain/test.txt | grep -E -o "[^/]+/?$"
ifconfig eth0 | egrep -o "[1-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]"
egrep "(^[[:alnum:]]+\>).*\1$" /etc/passwd