1. 典型的应用场合:grep、egrep检索文本行

       使用不带-E 选项的grep时,支持基本正则匹配。例如,'abc'关键词检索,'^abc' 匹配行首,'abc$' 匹配行尾

grep 'bash' /etc/passwd        #输出包含bash的行

grep '^r'  /etc/passwd         #匹配以r开头的行

grep 'in$' /etc/passwd         #匹配以in结尾的行

若是检索多个条件,例如,输出以'root'或者以'daemon'开头的行,在基本正则中,或者'|'必须转义为‘\|’

grep  '^root|^daemon'  /etc/passwd          #搜索无结果

grep  '^root\|^daemon'  /etc/passwd         #正确获得结果

使用grep -E 或者egrep 命令,可以支持扩展正则匹配,能自动识别' | '等正则表达式中的特殊字符,例如:

grep -E '^root|^daemon'  /etc/passwd

egrep '^root|^daemon'  /etc/passwd

2. grep、egrep 命令的-q选项

    选项-q  表示quiet(静默)的意思,结合此选项可以只做检索而不输出,通常在脚本内用来识别查找的目标是否存在,通过

返回状态%? 来判断,这样可以忽略无关的文本信息,简化脚本输出

    检查系统是否有www用户信息,如果存在则显示YES,否则输出NO,:

[root@localhost ~]# grep 'www' /etc/passwd && echo "YES" || echo "NO"
www:x:1002:1002::/home/www:/sbin/nologin
YES

加上-q 选项之后:

[root@localhost ~]# grep -q 'www' /etc/passwd && echo "YES" || echo "NO"
YES

可以忽略输出信息,只显示判断结果,容易辨别,-q 选项的效果与使用 &>  /dev/null 类似

3. -c 选项,输出匹配的行数

统计用户中登录shell为"/sbin/nologin" 的用户个数

[root@localhost ~]# grep -c 'sbin/nologin$' /etc/passwd
48

   通过-c 选项可输出匹配行数,与 wc -l 的效果相同,但是写法简单,例如:统计可登录系统的用户个数

[root@localhost ~]# grep -c '/bin/bash' /etc/passwd
4

或者

[root@localhost ~]# grep '/bin/bash' /etc/passwd | wc -l
4

4. 基本元字符

  " . "       匹配任意单个字符

输出/etc/rc.local 文件内至少包括一个字符的行,即非空行:

[root@localhost ~]# grep '.' /etc/rc.local 
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local

输出空行(-v 取反):

[root@localhost ~]# grep -v '.' /etc/rc.local

上述取空行的操作与下列操作效果相同

[root@localhost ~]# grep '^$' /etc/rc.local

+ 、?、*      目标出现的次数

输出包含" f "的行,即" f "至少出现一次:

[root@localhost ~]# egrep 'f+' /etc/rc.local 
# to run scripts during boot instead of using this file.
# this script will NOT be run after all other services.

输出包含init、initial的行,即末尾的' ial ' 最多出现一次:

[root@localhost ~]# egrep 'init(ial)?' /etc/rc.local
 # This script will be executed *after* all the other init scripts.
 # You can put your own initialization stuff in here if you don't
 # want to do the full Sys V style init stuff.

输出包括stu、stuf、stuff、stufff.……的行,即末尾的f可以出现0次或任意多次。重复目标只有一个字符时,可以不用括号:

[root@localhost ~]# egrep 'stuf*' /etc/rc.local
# You can put your own initialization stuff in here if you don't
 # want to do the full Sys V style init stuff.

输出所有行,单独的 ' .* ' 可以匹配任意行(包括空行):

[root@svr5 ~]# egrep '.*' /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

输出/etc/passwd内以 ' r ' 开头且以' nologin ' 结尾的行:

[root@localhost ~]# egrep '^r.*nologin$' /etc/passwd
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

{ }           限定出现的次数范围

egrep '(ab){3}' a.txt          #输出ab连续出现三次的行,即ababab

egrep '(ab){2,4}' a.txt        #输出ab连续出现2~4次的行,即abab,ababab,abababab

egrep '(ab){3,}' a.txt         #输出ab至少出现三次的行,即ababab,abababab,……

[ ]        匹配范围内的单个字符

输出包括abc、abd的行,即前两个字符为ab,第三个字符只要是c、d中的一个就符合条件

egrep 'ab[cd]' a.txt

输出非字母的行

egrep '[^a-zA-Z]' a.txt

\<    \>      单词边界

输出/etc/rc.local 内包括单词 ' init ' 的行,initialization不合要求:

[root@localhost ~]# egrep '\<init\>' /etc/rc.local
# This script will be executed *after* all the other init scripts.
# want to do the full Sys V style init stuff.

输出以" ll " 结尾的单词的行,使用 \> 匹配单词右边界:

[root@localhost ~]# egrep 'll\>' /etc/rc.local
 # This script will be executed *after* all the other init scripts.
 # want to do the full Sys V style init stuff.