1.显示/etc/passwd文件中以bash结尾的行
[root@localhost ~]# grep "bash$" /etc/passwd


2.显示/etc/passwd文件中的两位数或三位数
[root@localhost ~]# grep -o "\<[[:digit:]]\{2,3\}\>" /etc/passwd

3.显示`netstat -tan`命令结果中以‘LISTEN’后跟0个、1个或者多个空白字符结尾的行
[root@localhost ~]# netstat -tan |grep "LISTEN.*$"
  

4.添加用户bash、testbash、basher以及nologin用户(nologin用户的shell为/sbin/nologin);而后找出/etc/passwd文件中用户名与其shell名相同的行
[root@localhost ~]# useradd bash
[root@localhost ~]# useradd basher
[root@localhost ~]# useradd -s /sbin/nologin nologin
[root@localhost ~]# grep "^\(\<[[:alnum:]]\+\>\).*\1$" /etc/passwd
 

5.显示当前系统上root、centos或者user1用户的默认shell和UID (请事先创建这些用户,若不存在)
[root@localhost ~]# useradd centos
[root@localhost ~]# useradd user1
[root@localhost ~]# cat /etc/passwd |cut -d: -f1,3,7 |egrep "^root|centos|user1"

6.找出/etc/rc.d/init.d/functions文件中某单词(单词中间可以存在下划线)后面跟着一组小括号的行
[root@localhost ~]# egrep "\<[[:alpha:]_]+\>\(\)" /etc/rc.d/init.d/functions


7.使用echo输出一个路径,而后egrep找出其路径基名;进一步的使用egrep取出其目录名
[root@localhost ~]# echo /etc/rc.d/init.d/functions |egrep "([^/][[:alnum:]]+)$"   取出路径基名

[root@localhost ~]# echo /etc/rc.d/init.d/functions |egrep ".*/"                   取出目录名



8.找出ifconfig命令执行结果中1-255之间的数字
[root@localhost ~]# ifconfig |egrep "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]\>)"