grep命令:

    grep:默认支持基本正则表达式
    egrep:支持基本和扩展正则表达式
    fgrep:不支持正则表达式元字符,搜索字符串速度快
    
功能:查找文件里符合条件的行
基本用法:
        grep [-acinv] [--color=auto] [-A n] [-B n] '搜寻字符串' 文件名
            -a:将二进制文档以文本方式处理
            -c:显示匹配次数
            -i:忽略大小写差异
            -n:在行首显示行号
            -A:After的意思,显示匹配字符串后n行的数据
            -B:before的意思,显示匹配字符串前n行的数据
            -v:显示没有匹配的行
            --color:以特定颜色高亮显示匹配关键字

基本正则表达式:

元字符:

      ^:锚定行首  用法格式:"^pattern"
             grep --color=auto '^root' /etc/passwd
      $:锚定行尾  用法格式:"pattern$"
             grep --color=auto 'sh$' /etc/passwd
             grep --color=auto '^root$' /etc/passwd   #查找以root自成一行的行
             grep '^$' /etc/rc.d/rc.sysinit | wc -l   #统计空白行的行数
      .:匹配任意单个字符
      *:匹配紧挨在其前面的字符任意次(包含零次)
        a*b    #b之前任意个a
      .*:任意长度的任意字符
         grep --color=auto "r.*h" /etc/passwd
         grep --color=auto "r.*h$" /etc/passwd  #以r开头,h结尾的行
      []:匹配指定范围内的任意单个字符
          [a-z]       小写字母
          [A-Z]       大写字母
          [0-9]       数字
          [:lower:]   小写字母
          [:upper:]   大写字母
          [:digit:]   数字
          [:alpha:]   所有字母
          [:alnum:]   所有数字和字母
          [:space:]   空白字符
          [:punct:]   标点符号
      [^]:匹配指定范围外的任意单个字符
       \?:匹配紧挨在其前面的字符0次或1次
            a\?b   #b前面出现0个或1个a
      \{m,n\}:匹配其前面的字符至少m次,至多n次 
             \{0,3\}  至多3次
             \{3,\}   至少3次
             \{m\}    精确匹配m次
             grep "a\{1,2\}b" grep.txt  #b之前的a出现1次到2次
             grep "[bB].\{2,5\}[tT]"  /etc/rc.d/rc.sysinit  #以b(不区分大小写)开头,中间出现2到5次任意字符,后面跟t(不区分大小写)
       \<:锚定词首  用法格式:\<pattern
             grep "\<[bB].\{2,5\}[tT]" /etc/rc.d/rc.sysinit
       \>:锚定词尾  用法格式:pattern\>
             grep "[bB].\{2,5\}[tT]\>"  /etc/rc.d/rc.sysinit
             grep "\<[bB].\{2,5\}[tT]\>"  /etc/rc.d/rc.sysinit   #词首词尾都锚定 锚定单词

扩展正则表达式:

   grep默认情况下支持基本正则表达式,可以通过参数-E使其支持扩展正则表达式。另外egrep扩展了grep的功能,可以直接支持扩展正则表达式。这和grep -E是等价的。虽然一般情况下,基本正则表达式就已经够用了。特殊情况下,扩展正则表达式可以简化字符串的匹配。

    在基本正则表达式的基础上,扩展表达式只是增加了一些元字符。

    +:重复前面字符1到多次
       例如:匹配god,good,goood等等字符串。
               grep –nE go+d’ regular.txt
    |:或(or)的方式匹配多个字串
       例如:匹配/proc/meminfo文件中以s或S开头的行
               grep -nE "^(s|S)" /proc/meminfo
             匹配/root/test.txt文件中包含cat或Cat的行
               grep -nE "(c|C)at" /root/test.txt
   (): 分组 支持\1,\2 引用
       例如: 匹配出test.txt文件中包含west,中间包含任意字符,最后以es结尾的行
            [root@localhost tmp]# cat test.txt
            tsttst tsttsttst
            west gao
            west abces 
            [root@localhost tmp]# egrep "w(es)t.*\1" test.txt
            west abces
            [root@localhost tmp]# grep -E "w(es)t.*\1" test.txt
            west abces
            [root@localhost tmp]# grep "w\(es\)t.*\1" test.txt
            west abces
            
            //grep 也支持分组 只是需要对()本身进行转义 如果不转义 就会出现下面的错误:
            [root@localhost tmp]# grep "w(es)t.*\1" test.txt
            grep: Invalid back reference
{m,n}:匹配其前面的字符至少m次,至多n次
       在egrep使用时同样不需要转义{}本身、
       而在grep中使用时需要转义 \{m,n\}

练习题:

基础篇:
1,显示包含'the'的行
         # grep -n 'the' file.txt
2, 显示不包含'the'的行
         # grep -vn 'the' file.txt
3, 显示包含不区分大小写'the'的行
         # grep -in 'the' file.txt
4, 显示包含test或tast的行
         # grep -n 't[ae]st' file.txt
5, 显示以'the'开头的行
         # grep -n '^the' file.txt
6, 显示包含数字的行
         # grep -n '[0-9]' file.txt
7, 显示以小写字母开头的行
         # grep -n '^[a-z]' file.txt
8, 显示不以字母开头的行
         # grep -n '^[^a-zA-Z]' file.txt
9, 显示以点号结尾的行
         # grep -n '\.$' file.txt
10,显示出空白行的行数
         # grep '^$' file.txt | wc -l
11,去掉空白行与#号开头的行
         # grep -v '^$' file.txt | grep -v '^#'
         # egrep -v '^$|^#' file.txt
12,显示包含g后面跟2个或2个以上的o,再接一个g的行
         # grep -n 'go\{2,\}g' file.txt
13,显示包含g后面跟2到5个o,再接一个g的行
         # grep -n 'go\{2,5\}g' file.txt
14,显示/etc下所有文件类型为链接文件的文件名
         # ls -l /etc | grep '^l'
进阶篇:
1,显示/proc/meminfo文件中以不区分大小写的s开头的行
   解法一:grep -E "^(s|S)" /proc/meminfo
   解法二:grep -i "^s" /proc/meminfo
   解法三:grep "^[sS]" /proc/meminfo
2,显示/etc/passwd中以nologin结尾的行(也就是不能登录系统的用户的个数)的行数
   解法一:grep "nologin$" /etc/passwd  | wc -l
   解法二:grep "nologin\>" /etc/passwd | wc -l
3, 显示shell为bash,且其UID号最小的用户的用户名
   解法一:grep "bash$" /etc/passwd | sort -t: -k3 | head -1 | cut -d: -f1
   解法二:grep "bash\>" /etc/passwd | sort -t: -k3 | head -1 | cut -d: -f1
4, 显示/boot/grub/grub.conf文件中以一个或多个空白字符开头的行
   解法一:grep "^[[:space:]]\{1,\}" /boot/grub/grub.conf
   解法二:grep -E "^[[:space:]]+" /boot/grub/grub.conf
5,ifconfig eth0 只取出IP地址
   解法一:ifconfig eth0 | grep -o 'inet addr:[^[:space:]]*' | cut -d: -f2
   解法二:ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | cut -d' ' -f1
   解法三:ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'
   解法四:ifconfig eth0 | grep 'inet addr' | awk -F '[ :]' '{print $13}'
       注:这个是将[空格:]组合来分割 IP地址在13列的位置
   解法五:ifconfig eth0 | awk -F '[ :]+' 'NR==2 {print $4}'
       注:NR==2 表示显示第二行的意思
6, 查找当前系统上名字为student(必须出现在行首)的用户的账户的相关信息
   解法一:grep "^student:" /etc/passwd
   解法二:grep "^student\>" /etc/passwd