zoukankan      html  css  js  c++  java
  • grep命令

    准备test文件,内容如下:

     

    dlf test

    dlfthink

    www.baidu.com

    TEST 123

    Dlf's lemon

    grep Grep

    abc

    abc123abc

    123dlf123

    [www.baidu.com]

    1)      grep -i "test" test.txt  搜索出含有“test” 字符串(-i:不区分大小)

     

    2)      grep -i -n "test" test.txt  搜索出含有“test” 字符串(-i:不区分大小),并打印行号

     

    3)      grep -i -n --color "test" test.txt  搜索出含有“test” 字符串(-i:不区分大小),并打印行号,关键字“test”颜色标记(centos7系统默认为grep命令配置了别名,所以不使用—color也能显示颜色)

     

    4)      grep -I -c "test" test.txt,打印“test”字符串(不区分大小写)显示的次数

     

    5)      grep -I -o "test" test.txt,打印“test”字符串(不区分大小写),但不打印整行

     

    test2.txt文件内容如下:

     

    6)      grep -A1 “18” test2.txt,打印“18”字符串和它上一行的信息

     

    7)      grep -B1 “18” test2.txt,打印“18”字符串和它下一行的信息

     

    8)      grep -C1 “18” test2.txt,打印“18”字符串和它上一行和下一行的信息

     

    9)      grep -w "dlf" test.txt,精确匹配出“dlf”字符串的

     

    10)   grep -v "dlf" test.txt,匹配出不包含“dlf”字符串的行

     

    11)   grep -e 'abc' -e 'dlf' test.txt,同时匹配”abc”和”test”字符串

     

    12)   grep -q 静默模式

    静默模式下grep不会输入任何信息,无论是否匹配到指定的字符串,都不会输出任何信息,所以配合使用“echo %?”命令,查看命令的执行状态,如果返回值0,证明上一条grep命令匹配到了指定的字符串,如果返回值1,则证明上一条grep命令没有匹配到指定的字符串。

     

    参考来源

    http://www.zsythink.net/archives/1733

  • 相关阅读:
    判断 undefined and ( == null) and (!something) and ( == null)
    textarea高度自适应自动展开
    退出 js和Jquery区别
    javascript高级程序设计 学习笔记 第五章 下
    Bind, Call and Apply in JavaScript
    javascript高级程序设计 学习笔记 第五章 上
    小程序入门---登录流程
    Array类型 JS
    深入浅出妙用 Javascript 中 apply、call、bind
    微信公众号开发(与angular框架相结合)
  • 原文地址:https://www.cnblogs.com/lemon-feng/p/11249461.html
Copyright © 2011-2022 走看看