1、grep(Global search REgular expression and Print out the line),即全局搜索正则表达式并打印出匹配的行,它是Linux系统中一个强大的文本搜索工具,它根据用户指定的“模式(pattern)”对目标文本进行过滤,显示被模式匹配到的行;
2、正则表达式是由一类字符书写的模式,其中有些字符不表示符的字面意义,而是表示控制或通配的功能
\(\) : 对某字符串进行分组匹配
例如:匹配xy单词出现0次或1次的行
[root@localhost ~]# grep '\(xy\)\?' grep_test.txt
y->1line
xy->2line
xxy->3line
xxxy->4line
aby->5line
abcy->7line
xyxy->8linexyxy
xxxy->9line
xy->10line
xxx->11linexy
[root@localhost ~]# 后向引用:模式中,如果使用\(\)实现了分组,在某行文本的检查中,如果\(\)的模式匹配到了某内容,此内容后面的模式中可以被引用;
对前面的分组进行引用的符号为:\1 , \2 ,\3
模式自左而右,引用第#个左括号以及与其匹配右括号之间的模式匹配到的内容;
后向引用举例:
新建一个文本文件,假设有如下内容:
[root@localhost ~]# cat grep.txt
He like his lover.
He love his love.
He like his liker.
He like his like.
[root@localhost ~]#例如:找出前后相同单词的行:
[root@localhost ~]# grep '\(\<[[:alpha:]].*\>\).*\<\1\>' grep.txt
He love his love.
He like his like.
[root@localhost ~]#
四、egrep及扩展正则表达式:
egrep相当于grep -E,egrep可以直接使用扩展正则表达式,而grep需要加上选项-E;
扩展正则表达式的元字符:
字符匹配:. ,[] ,[^]
次数匹配:*,?,+,{m},{m,n},{m,},{0,n}
位置锚定:^,$,\>,\<
分组匹配:(),支持后向引用
| : 匹配左侧或右侧符合条件的行,比如a|b,含有a或b的行都匹配;
例如:egrep等同于grep -E
[root@localhost ~]# grep -E 'k|i' grep.txt
He like his lover.
He love his love.
He like his liker.
He like his like.
[root@localhost ~]# egrep 'k|i' grep.txt
He like his lover.
He love his love.
He like his liker.
He like his like.
[root@localhost ~]#
五、grep练习题:
(1).显示/proc/meminfo文件中以大写或小写s开头的行;
# grep -i '^s' /proc/meminfo
(2).显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;
# grep -v '/sbin/nologin$' /etc/passwd | cut -d: -f1
(3).显示/etc/passwd文件中其默认shell为/bin/bash的用户
进一步:仅显示上述结果中其ID号最大的用户
# grep '/bin/bash$' /etc/passwd | cut -d: -f1 | sort -n -r | head -1
(4).找出/etc/passwd文件中的一位数或两位数;
# grep '\<[[:digit:]]\{1,2\}\>' /etc/passwd
(5).显示/boot/grub/grub.conf中至少一个空白字符开头的行
# grep '^[[:space:]]\+.*' /boot/grub/grub.conf
(6).显示/etc/rc.d/rc.sysinit文件中,以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;
# grep '^#[[:space:]]\+[^[:space:]]\+' /etc/rc.d/rc.sysinit
(7).找出netstat -tan命令执行结果中包含'LISTEN'的行;
# netstat -tan | grep 'LISTEN[[:space:]]*$
(8).添加用户bash,testbash,basher,nologin(SHELL为/sbin/nologin),而找出当前系统上其用户名和默认SHELL相同的用户;
# grep '\(\<[[:alnum:]]\+\>\).*\1$' /etc/passwd
(9).扩展题:新建一个文本文件,假设有如下内容:
He like his lover.
He love his lover.
He like his liker.
He love his liker.
找出其中最后一个单词是由此前某单词加r构成的行;
# grep '\(\<[[:alpha:]]\+\>\).*\1r' grep.txt
(10).显示当前系统上root、centos或user1用户的默认shell及用户名;
# grep -E '^(root|centos|user1\>)' /etc/passwd
(11).找出/etc/rc.d/init.d/functions文件中某单词后面跟一对小括号'()"的行;
# grep -o '\<[[:alpha:]]\+\>()' /etc/rc.d/init.d/functions
(12).使用echo输出一个路径,而使用egrep取出其基名;
# echo /etc/rc.d/ | grep -o '[^/]\+/\?$' | grep -o '[^/]\+'