显示/etc/rc.d/rc.sysinit文件中以不区分大小的h开头的行
#因没有/etc/rc.d/rc.sysinit文件,故换另外的文件进行练习
[root@William test]# grep -i "h" /etc/rc.d/rc.local
显示/etc/passwd中以sh结尾的行
[root@William test]# grep "sh$" /etc/passwd
显示/etc/fstab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行
[root@William test]# grep -E "^#[[:space:]]+[^[:space:]]+" /etc/fstab
#”+“:表示对前面的一项进行一次或者多次重复的匹配
查找/etc/rc.d/rc.local中包含“以to开始并以to结尾”的字串行
[root@William test]# grep -E -w "(to).*\1" /etc/rc.d/rc.local
#"-w":表示匹配一个单词,"(to).*\1"将to这个单词匹配多次
查找/etc/inittab中含有“以s开头,并以d结尾的单词”模式的行
[root@William test]# grep -w '\<s[a-Z]*d\>' /etc/inittab
查找ifconfig命令结果中的1-255之间的整数
[root@William test]# ifconfig | grep -E "([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
#显示1-255之间整数的行
[root@William test]# ifconfig | grep -E -o "([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
#显示ifconfig命令结果中出现的数字
显示/var/log/secure文件中包含“Failed”或“FAILED”的行
[root@William test]# grep "(Failed|FAILED)" /var/log/secure
在/etc/passwd中取出默认shell为bash的行
[root@William test]# grep "bash" /etc/passwd
以长格式列出/etc/目录下以ns开头、.conf结尾的文件信息
[root@William test]# ls -l /etc/ns*.conf
高亮显示passwd文件中冒号,及其两侧的字符
[root@William test]# grep -E ".?:*:.?" /etc/passwd
#".":代表一个字符;"?":表示匹配0次或1次