egrep = grep -E

  egrep [OPTIONS] PATTERN [FILE..]

  扩展正则表达式的元字符

   字符匹配

      .: 匹配任意单个字符

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

                      [^]: 匹配指定范围外的单个字符

   次数匹配:

      *: 匹配前面的字符任意次

      ?: 匹配前面的字符0次或1次

      +: 匹配前面的字符1次或多次

      {m}: 匹配前面的字符m次

      {m,n}: 匹配前面的字符至少m次,至多n次

   锚定:

      ^: 锚定行首

      $: 锚定行尾

      \<或\b: 锚定词首

      \>或\b: 锚定词尾

   分组

      (): 括号中的字符串作为分组,并后向引用,\1,\2,...

   或者

      a|b: 例如C|cat 表示C或cat

复制代码
        
练习:
1、显示当前系统root、centos、user1用户的默认shell和UID
[root@localhost ~]# grep -E "^(root|centos|user1)\>" /etc/passwd | cut -d: -f3,7
0:/bin/bash
707:/bin/bash
708:/bin/bash

2、找出/etc/rc.d/init.d/functions文件(centos)中某单词后面跟一个小括号的行
[root@localhost ~]# grep -E -o "[_[:alpha:]]+\(\)" /etc/rc.d/init.d/functions

3、使用echo输出一绝对路径,使用egrep取出器路径基名;并且使用egrep取出路径的目录名,类似于dirname命令的结果
[root@localhost ~]# echo "/etc/passwd" | grep -E -o "[^/]+/?$" | cut -d"/" -f1
[root@localhost ~]# echo "/etc/passwd/" | grep -E -o "[^/]+/?$" | cut -d"/" -f1

4、找出ifconfig命令结果中1-255之间的数值
ifconfig | grep -E --color=auto "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"

5、找出ifconfig命令结果中的IP地址
View Code
复制代码

   fgrep ""

      不支持正则表达式搜索,是什么就找什么,快速搜索机制
      [root@localhost ~]# fgrep --color=auto "root" /etc/passwd
      root:x:0:0:root:/root:/bin/bash
      operator:x:11:0:operator:/root:/sbin/nologin