设为首页 收藏本站
查看: 339|回复: 0

[经验分享] grep命令最常用的功能总结

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-11-5 09:35:27 | 显示全部楼层 |阅读模式
1. grep最简单的使用方法,匹配一个词:grep word filename

2. 可以从多个文件中匹配:grep word filename1 filenam2 filename3

3. 可以使用正则表达式匹配:grep -E pattern f1 f2 f3...

4. 可以使用-o只打印匹配的字符,如下所示:



    lichao@ubuntu:command$ echo this is a line. | grep -E -o "[a-z]*\."  
    line.  

5. 打印除匹配行之外的其他行,使用-v



    lichao@ubuntu:command$ echo -e "1\n2\n3\n4" | grep -v -E "[1-2]"  
    3  
    4  


6. 统计匹配字符串的行数,使用-c



    lichao@ubuntu:command$ echo -e "1111\n2222" | grep -E "[1-2]" -c  
    2  


7. 如果我们统计字符串模式匹配的次数,可以结合-o和-c,如下:



    lichao@ubuntu:command$ echo -e "1111\n2222" | grep -o -E "[1-2]"  | wc -l  
    8  


8. 如果需要显示行号,可以打开-n,如下:



    lichao@ubuntu:command$ echo -e "1111\n2222\n33333\n44444" | grep -n -E "3"  
    3:33333  


9. -b选项可以打印出匹配的字符串想对于其所在的行起始位置的偏移量(从0开始),通常配合-o使用,如下:



    lichao@ubuntu:command$ echo "0123456789" | grep -b -o 4  
    4:4  


10. 当字符串在多个文件中匹配时,-l选项将只打印文件名


11. -L与-l相对,只打印不匹配的文件名



    lichao@ubuntu:command$ cat test1.txt  
    linux  
    is  
    fun  
    lichao@ubuntu:command$ cat test2.txt  
    a   
    very  
    popular   
    os,  
    linux  
    lichao@ubuntu:command$ cat test3.txt  
    what  
    the  
    fxxk  
    lichao@ubuntu:command$ grep -l linux test1.txt test2.txt test3.txt  
    test1.txt  
    test2.txt  
    lichao@ubuntu:command$ grep -L linux test1.txt test2.txt test3.txt  
    test3.txt  


12. 打开递归搜索功能



    lichao@ubuntu:command$ grep -n -R linux .   
    ./test2.txt:5:linux  
    ./test1.txt:1:linux  


13. 忽略大小写:-i



    lichao@ubuntu:command$ echo "HELLO WORLD" | grep -i "hello"  
    HELLO WORLD  


14. 匹配多个字符串模式



    lichao@ubuntu:command$ echo "This is a line." | grep -e "This" -e "is" -e "line" -o  
    This  
    is  
    line  


15. 用单独的文件提供匹配样式,每个匹配的样式作为一行,如下例所示:



    lichao@ubuntu:command$ cat pattern.txt  
    1$  
    2  
    3  
    lichao@ubuntu:command$ cat num.txt   
    1  
    2  
    3  
    4  
    5  
    6  
    7  
    8  
    9  
    10  
    lichao@ubuntu:command$ grep -f pattern.txt num.txt   
    1  
    2  
    3  


16. 打印匹配行上下文信息,使用 -A n打印匹配行及其后n行信息;使用-B n打印匹配行及其前n行信息;使用 -C n,打印匹配行及其前后n行信息;如果有多重匹配,将使用--隔离。示例如下:



    lichao@ubuntu:command$ seq 1 10 | grep 5 -A 3  
    5  
    6  
    7  
    8  
    lichao@ubuntu:command$ seq 1 10 | grep 5 -B 3  
    2  
    3  
    4  
    5  
    lichao@ubuntu:command$ seq 1 10 | grep 5 -C 3  
    2  
    3  
    4  
    5  
    6  
    7  
    8  
    lichao@ubuntu:command$ echo -e "a\nb\nc\nd\na\nb\nc\nd\n" | grep a -A 2  
    a  
    b  
    c  
    --  
    a  
    b  
    c  


17. 使用-q进入静默模式,该模式下,grep命令运行目的仅仅是执行一个条件测试,通常在脚本中使用。通过检查其返回值进行下一步操作。示例如下:



    lichao@ubuntu:command$ cat tmp.txt  
    hello  
    world  
    lichao@ubuntu:command$ cat tmp.csh  
    #!/bin/bash  
    if [ $# -ne 2 ]; then  
        echo "Usage: $0 match_pattern file_name"  
        exit  
    fi  
    match=$1  
    file=$2  
    grep -q $match $file  
    if [ $? -ne 0 ]; then  
        echo "$match not exist in $file"  
    else  
        echo "$match exist in $file"  
    fi  
    lichao@ubuntu:command$ ./tmp.csh hello tmp.txt  
    hello exist in tmp.txt  


18. -Z选项在输出匹配文件名时将以/0结尾配合xargs -0可以发挥很多作用,例如删除匹配某个模式的文件如下:



    lichao@ubuntu:command$ ls -llrt  
    total 28  
    -rw-rw-r-- 1 lichao lichao  13 Nov  1 20:38 test1.txt  
    -rw-rw-r-- 1 lichao lichao  27 Nov  1 20:39 test2.txt  
    -rw-rw-r-- 1 lichao lichao  14 Nov  1 20:39 test3.txt  
    -rw-rw-r-- 1 lichao lichao  21 Nov  1 20:45 num.txt  
    -rw-rw-r-- 1 lichao lichao   7 Nov  1 20:45 pattern.txt  
    -rw-rw-r-- 1 lichao lichao  12 Nov  1 21:25 tmp.txt  
    -rwxr-xr-x 1 lichao lichao 217 Nov  1 21:27 tmp.csh  
    lichao@ubuntu:command$ cat test1.txt  
    linux  
    is  
    fun  
    lichao@ubuntu:command$ cat test2.txt  
    a   
    very  
    popular   
    os,  
    linux  
    lichao@ubuntu:command$ grep "linux" * -lZ | xargs -0 rm  
    lichao@ubuntu:command$ ls  
    num.txt  pattern.txt  test3.txt  tmp.csh  tmp.txt  

以上命令将包含linux字符串的test1.txt和test2.txt删除。


19. 排除/包括文件或者目录:1)--include *{.c,.cpp} 只在目录中搜索.c和.cpp文件;2)--exclude "README" 排除所有README文件 3) --include-dir 仅在某些目录中搜索 4) --exclude-dir 排除某些目录 5) --exclude-from FILE 从文件FILE中读取需要排除的文件列表



    lichao@ubuntu:test$ ls  
    dir1  dir2  exclude.config  test1.txt  test2.doc  test3.word  
    lichao@ubuntu:test$ cat test1.txt   
    linux   
    is   
    fun  
    lichao@ubuntu:test$ cat test2.doc   
    wonderful   
    os,  
    linux  
    lichao@ubuntu:test$ cat test3.word   
    wonderful   
    os,  
    linux  
    lichao@ubuntu:test$ ls dir1/  
    test1.txt  test2.doc  test3.word  
    lichao@ubuntu:test$ ls dir2/  
    test1.txt  test2.doc  test3.word  
    lichao@ubuntu:test$ cat exclude.config   
    *.txt  
    lichao@ubuntu:test$ grep "linux" -R -n .   
    ./test2.doc:3:linux  
    ./test3.word:3:linux  
    ./test1.txt:1:linux   
    ./dir2/test2.doc:3:linux  
    ./dir2/test3.word:3:linux  
    ./dir2/test1.txt:1:linux   
    ./dir1/test2.doc:3:linux  
    ./dir1/test3.word:3:linux  
    ./dir1/test1.txt:1:linux   
    lichao@ubuntu:test$ grep "linux" -R -n . --include *.txt --include *.doc  
    ./test2.doc:3:linux  
    ./test1.txt:1:linux   
    ./dir2/test2.doc:3:linux  
    ./dir2/test1.txt:1:linux   
    ./dir1/test2.doc:3:linux  
    ./dir1/test1.txt:1:linux   
    lichao@ubuntu:test$ grep "linux" -R -n . --exclude *.txt --eclude *.doc  
    grep: unrecognized option '--eclude'  
    Usage: grep [OPTION]... PATTERN [FILE]...  
    Try 'grep --help' for more information.  
    lichao@ubuntu:test$ grep "linux" -R -n . --exclude *.txt --exclude *.doc  
    ./test3.word:3:linux  
    ./dir2/test3.word:3:linux  
    ./dir1/test3.word:3:linux  
    lichao@ubuntu:test$ grep "linux" -R -n . --exclude-dir dir1  
    ./test2.doc:3:linux  
    ./test3.word:3:linux  
    ./test1.txt:1:linux   
    ./dir2/test2.doc:3:linux  
    ./dir2/test3.word:3:linux  
    ./dir2/test1.txt:1:linux   
    lichao@ubuntu:test$ grep "linux" -R -n . --exclude-dir dir1 --exclude-dir dir2  
    ./test2.doc:3:linux  
    ./test3.word:3:linux  
    ./test1.txt:1:linux   
    lichao@ubuntu:test$ grep "linux" -R -n . --exclude-from exclude.config   
    ./test2.doc:3:linux  
    ./test3.word:3:linux  
    ./dir2/test2.doc:3:linux  
    ./dir2/test3.word:3:linux  
    ./dir1/test2.doc:3:linux  
    ./dir1/test3.word:3:linux  


已上即为grep常用的选项。


注意:转载请注明出处。





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-27120-1-1.html 上篇帖子: centos的软件管理工具RPM和yum 下篇帖子: ubuntu桌面版系统安装后切换到root用户并且设置root密码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表