• shell 三剑客之 grep


    grep 的全称是 Globally search a Regular Expression and Print,是一种强大的文本搜索工具,它能使用特定模式匹配(包括正则表达式)搜索文本,并默认输出匹配行

    我们程序员最常用的是查找指定的线程:

    去查找指定的进程:ps -ef | grep *** 
    

    grep提供两种方式:

    grep [option] [pattern] testfile
    stdout | grep [option] [pattern]
    
    语法格式 解释
    option 参数选项
    pattern 为查找的字符串或者正则表达式

    第一种是从文本中直接使用pattern匹配搜索
    第二种是从标准输出中处理 grep所提供的option(参数选项)

    参数选项 含义
    -i 搜索时略大小写
    -n 显示行号
    -o 只显示被匹配的关键字,不会打印匹配的整行内容
    -r 递归搜索
    -v 不显示匹配行信息
    -q 静默模式,不输出任何信息,在 shel 脚本中,可以通过 echo$?查看是否匹配到,0 表示匹配到,1 表示没有匹配到
    -E 使用扩展正则表达式

    接下来测试一下:

    # 创建一个文件,内容如下:
    [root@master tmp_test]# cat test.txt
    test
    math test
    sunday
    sunny
    java
    python
    shell
    ruby Test
    
    # 查找 test
    [root@master tmp_test]# grep  "test" test.txt
    test
    math test
    发现 grep 区分大小写,Test 就没有查出来。
    
    # 不区分大小写
    [root@master tmp_test]# grep  -i "test" test.txt
    test
    math test
    ruby Test
    
    # 把行号打印出来
    [root@master tmp_test]# grep  -i -n "test" test.txt
    1:test
    2:math test
    8:ruby Test
    
    # 只想查看关键字,行内其他内容忽略
    [root@master tmp_test]# grep  -i -n -o "test" test.txt
    1:test
    2:test
    8:Test
    
    # 查看当前文件夹下多个文件里面字符匹配
    # 拷贝一份并充命名
    [root@master tmp_test]#  cp test.txt  test1.txt
    # 退回到上一级目录
    [root@master tmp_test]# cd ..
    # 搜索当前文件夹内的文字匹配
    [root@master home]# grep -r -i -n "test" tmp_test/
    tmp_test/test1.txt:1:test
    tmp_test/test1.txt:2:math test
    tmp_test/test1.txt:8:ruby Test
    tmp_test/test.txt:1:test
    tmp_test/test.txt:2:math test
    tmp_test/test.txt:8:ruby Test
    
    # -q 表是静默静默模式,在此模式下grep命令不会有任何的打印结果,无论是否有匹配到。
    
    #通过我们会把结果输出到一个文件中
    [root@master home]# grep -r -i -n  "test" tmp_test/ >> tmp_test/result
    
    # 实际编写脚本时,需要判断是否有输出数据,用 $? 来判断
    [root@master tmp_test]# grep -i -n -q "test" test.txt
    [root@master tmp_test]# echo $?
    0
    [root@master tmp_test]# grep -i -n -q "teeeest" test.txt
    [root@master tmp_test]# echo $?
    1
    
    0 表是有输出数据,1表示没有输出数据。
    
    

    grep 结合正则表达式:
    上面说了,grep 的全称是 Globally search a Regular Expression and Print。
    grep 之所以强大,就是因为可是使用正则。

    基本正则表达式

    语法 含义
    . 单个字符
    * 表示前面的字符连续出现任意次,包括0次
    ^ 表示锚定行首
    $ 表示锚定行尾
    [a-z] [0-9] 区间范围

    扩展正则,匹配是需要加 -E

    语法 含义
    表示匹配其前面的字符0或1次
    + 表示匹配其前面的字符至少1次,或者连续多次,连续次数上不封顶。
    () 分组
    {} 连续匹配
    | 匹配多个表达式的任何一个
    #查询 以“test”开头的
    [root@master tmp_test]# grep "^test" test.txt
    test
    
    #查询 以“test”结尾的
    [root@master tmp_test]# grep "test$" test.txt
    test
    math test
    
    # 查询含有两个连续n,并且以y结尾的
    [root@master tmp_test]# grep -E "*n{2}y$" test.txt
    sunny
    
    

    grep 配合正则还可以做很多事情,有实际需要时记得及时查资料。

  • 相关阅读:
    [LeetCode] 240
    [LeetCode] 169
    [LeetCode] 28
    [LeetCode] 27
    [LeetCode] 14
    [LeetCode] 9
    [LeetCode] 7
    [LeetCode] 2
    数据库开发规范
    Mysql优化
  • 原文地址:https://www.cnblogs.com/bigband/p/13538510.html
Copyright © 2020-2023  润新知