Example1、显示/etc/passwd文件中不以/bin/bash结尾的行;

~]# grep -v "/bin/bash$" /etc/passwd
~]# grep -v "/bin/bash\>" /etc/passwd

Example2、找出/etc/passwd文件中的两位数或三位数;

[root@CentOS7-171 ~]# grep "[0-9]\{2,3\}" /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
...以下省略...

Example3、找出/etc/grub2.cfg文件中,以至少一个空白字符开头,且后面非空白字符的行;

[root@CentOS7-171 ~]# grep "^[[:space:]]\+[^[:space:]]" /etc/grub2.cfg
  load_env
   set default="${next_entry}"
   set next_entry=

Example4、找出"netstat -tan"命令的结果中以'LISTEN'后跟0、1或多个空白字符结尾的行;

[root@CentOS7-171 ~]# netstat -tan | grep "LISTEN[[:space:]]*$"
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::8009                 :::*                    LISTEN