grep用法

grep 选项

    -c :只输出匹配行的计数。

    -i :忽略大小写(只适用于单字符)

    -n :显示匹配行及行号

    -v :取反(显示不匹配的行)

    -w :精确匹配某个字符

    -o :只显示被匹配到的字符串

   -A 显示匹配的行以及之后的N行

 

    -B 显示匹配的行以及之前的N行

 

    --no-group-separator

 

         当使用'-A', '-B' or '-C'时,不输出任何组分隔符,而是将不同组相邻输出。

 

    --color :以颜色的形式显示被匹配到字符

 

我们可以通过设置别名来方便我们的操作

alias grep='grep -n --color'

 

再次使用grep 效果如下:

[root@centos5 ~]# grep "root" /etc/passwd

1:root:x:0:0:root:/root:/bin/bash

11:operator:x:11:0:operator:/root:/sbin/nologin

关键词已经被着色并且显示行号

 

 

<1>[root@localhost etc]# grep -c "^root" passwd
         1
     这表示符合条件的行只有1行。
     <2>[root@localhost etc]# grep -n "^root" passwd
     1:root:x:0:0:root:/root:/bin/bash

 

     显示匹配行及行号     

 

     <3> -o -w的区别

 

     [root@localhost pangbing]# cat 1

 

          12

 

          123

 

          1234

 

          134

 

     -o 效果   

 

     [root@localhost pangbing]# grep -o '12' 1

 

                    12

 

                    12

 

                    12

 

     他把含有12的都过滤出来来了,但是多余的就去掉了

 

     -w效果

 

     [root@localhost pangbing]# grep -w '12' 1

 

           12

 

     他只匹配了12,精确匹配了

 

     

 

     -A效果

 

          [root@localhost pangbing]# grep  -w -A 1 '12' 1

 

          12

 

          123

 

        他将匹配的行的下一也打印出来

 

     -B效果

 

     [root@localhost pangbing]# grep  -w -B 1 '123' 1

 

          12

 

          123

  --no-group-separator

    [root@centos3 log]# cat abc

    123abcakshdjh fff

    qwe aaa

    asd bbb

    1111111111111

    sssssssssssssss

    123lzskjdfaklsjdkla fff

    aaa 123

    abv fvvv

    222222222222222222222

    aaaaaaaaaaaaaaaaaaaaa

    123dddddsadasdasd fff

    sdsdsd  asd

    sadsad  asdasd

    vvvvvvvvvvvvvvvvvv

 

  [root@centos3 log]# grep -A 2  "123" abc

    123abcakshdjh fff

    qwe aaa

    asd bbb

    --

    123lzskjdfaklsjdkla fff

    aaa 123

    abv fvvv

    222222222222222222222

    --

    123dddddsadasdasd fff

    sdsdsd  asd

    sadsad  asdasd

    此时过滤的结果当中有-- 分割符号,去掉这个符号加上--no-group-separator

    选项就没有了。

    grep -A 2 --no-group-separator "123" abc

 

 

通配符:

    *  :任意长度的任意字符

    :单个字符

    [] :范围之内

    [^] :范围之外的

 

二、正则表达式

    1. ():匹配任意单个字符

            匹配以r开头,t结尾中间只有两个字符的行

    例子:  [root@centos5 ~]# grep "r..t" /etc/passwd

            1:root:x:0:0:root:/root:/bin/bash

            11:operator:x:11:0:operator:/root:/sbin/nologin

            14:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

       

    2* :一个单字符后紧跟*,匹配0或个多个此单字符。(就是这个字符可以出现多次,也可以不出现。)

 

    3.* ; 表示任意长度的任意字符

    [root@centos5 ~]# cat abc

    a

    b

    ab

    aab

    acb

    adb

    amnb

   

    匹配a开头b结尾,中间是任意字符的行

    [root@centos5 ~]# grep "a.*b" abc

    ab

    aab

    acb

    adb

    amnb

其中ab中间没有字符也会被匹配到。任意长度的任意字符也包括空格

 

4a\{n\} : 用来匹配前面 a 的出现次数。n 为次数。

        [root@centos5 ~]# cat abc

        ab

        aab

        aaab

        aaaab

        aaaaaaab

 

        [root@centos5 ~]# grep --color "a\{1\}b" abc

        ab

        aab

        aaab

        aaaab

        aaaaab

       

        a只匹配了一次

5a\{n,\} : 用来匹配前面 a 的出现次数。但是次数至少为n次。

        [root@centos5 ~]# grep "a\{1,\}b" abc

        ab

        aab

        aaab

        aaaab

        aaaaab

        a至少出现1次,出现多次也可以被匹配到

6a\{n,m\} :用来匹配前面 a 的出现次数。但是a出现的次数在 n 和 m 之间

       

        [root@centos5 ~]# grep "a\{1,3\}b" abc

        ab

        aab

        aaab

        aaaab

        aaaaaaab

        这次匹配到的内容,只匹配了a最多3

 

三、正则中的字符集合

[:digit:]  表示数字

[:lower:]  表示小写字母

[:upper:]  表示大写字符

[:alpha:]  表示所有字符

[:punct:]  表示标点符号

[:alnum:]  表示所有数字和字符

在使用这些字符集合的时候,中括号外边必须在加一对中括号

 

例子:[root@centos5 ~]# cat test

    a

    ab

    abc

    12

    123

    1234

    a1

    ab12

    abc123

    Abc

    ABC123

要求1:过滤包含数字的行

[root@centos5 ~]# grep  "[[:digit:]]" test

12

123

1234

a1

ab12

abc123

ABC123

或是用这种方法也可以

grep -E  "[0-9]" test

 

 

要求2:包含字符的行

[root@centos5 ~]# grep "[[:alpha:]]" test

a

ab

abc

a1

ab12

abc123

Abc

ABC123

或是用这种方法也可以

grep -E  "[a-z]|[A-Z]" test

 

在脚本中应用

#!/bin/bash

read -p "请输入内容: " a

if [[ $a =~ [a-z]|[A-Z] ]];then

        echo "字符"

else

        echo "非字符"

fi

 

用字符集合的话将[a-z]|[A-Z]替换成[[:alpha:]]

#!/bin/bash

read -p "请输入内容: " a

if [[ $a =~ [[:alpha:]] ]];then

        echo "字符"

else

        echo "非字符"

fi

posted @ 2017-03-11 17:23  借风拥你  阅读(897)  评论(0编辑  收藏  举报