Linux三剑客之-grep

Linux三剑客之-grep

描述

grep – global search regular expression(RE) and print out the line;
grep 是一种文本搜索工具,它能使用正则表达式过滤或搜索文本并将匹配的行打印出来;

使用方式
  • 常用选项

    选项描述
    -A <n>显示过滤文本行的同时显示该行后的n行
    -B <n>显示过滤文本行的同时显示该行前的n行
    -C <n>显示过滤文本行的同时显示该行前后的各n行
    -c计算符合文本的行数
    -e用于指定多个字符串进行过滤
    -E使用扩展正则表达式进行过滤
    -f从指定的文件中获取字符串进行过滤,一行为一个过滤项
    -i忽略过滤字符的大小写
    -n标明符合过滤文本的行号
    -v翻转查找,显示不符合过滤文本的行
    -o仅输出匹配字符串
    -m <num>匹配num行结果后停止查找
    -q静默模式,不输出任何信息,用于脚本判断
  • 常用示例

    • 过滤匹配规则的文本行的同时显示其上下各两行
    [root@centos-36_2 tmp]# cat test_grep 
    The Redis server has been running, ignore this step.
    
    2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info
    2021-07-29 12:33:44-abc.py-542-INFO:  ===> rcp_msg_id: 2
    2021-07-29 13:15:15-kln.py-478-INFO: Received NotifyREQ
    
    2021-07-29 13:15:55-tor.py-550-NOTIFY: RPD IRA-RSP CORE-ID TLVs: 
    Index: 3
    CoreId: Broad0
    CoreIpAddress: 172.16.36.218
    IsPrincipal: false
    CoreName: broad_Aux
    
    2021-07-29 15:18:15-orr.py-553-ERR: Not Get RPD IRA-RSP
    [root@centos-36_2 tmp]#  grep -C 2 'abc' test_grep 
    
    2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info
    2021-07-29 12:33:44-abc.py-542-INFO:  ===> rcp_msg_id: 2
    2021-07-29 13:15:15-kln.py-478-INFO: Received NotifyREQ
    
    [root@centos-36_2 tmp]# 
    
    • 过滤匹配规则的文本行的同时显示行号
    [root@centos-36_2 tmp]# grep -n 'abc' test_grep 
    4:2021-07-29 12:33:44-abc.py-542-INFO:  ===> rcp_msg_id: 2
    [root@centos-36_2 tmp]# 
    
    • 显示过滤匹配规则的文本行的行数
    [root@centos-36_2 tmp]# grep -n 'NOTIFY' test_grep 
    3:2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info
    7:2021-07-29 13:15:55-tor.py-550-NOTIFY: RPD IRA-RSP CORE-ID TLVs: 
    [root@centos-36_2 tmp]# grep -c 'NOTIFY' test_grep 
    2
    [root@centos-36_2 tmp]# 
    
    • 设置多个匹配规则进行过滤
    [root@centos-36_2 tmp]# grep -e "INFO" -e "NOTIFY" test_grep 
    2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info
    2021-07-29 12:33:44-abc.py-542-INFO:  ===> rcp_msg_id: 2
    2021-07-29 13:15:15-kln.py-478-INFO: Received NotifyREQ
    2021-07-29 13:15:55-tor.py-550-NOTIFY: RPD IRA-RSP CORE-ID TLVs: 
    [root@centos-36_2 tmp]# 
    
    • 使用正则表达式的匹配规则进行过滤
    [root@centos-36_2 tmp]# grep -E "^Core.*[0-9]{1,3}$" test_grep 
    CoreId: Broad0
    CoreIpAddress: 172.16.36.218
    [root@centos-36_2 tmp]# grep -E "^Core.*[0-9]{2,3}$" test_grep 
    CoreIpAddress: 172.16.36.218
    [root@centos-36_2 tmp]# 
    
    • 过滤匹配规则时忽略大小写
    [root@centos-36_2 tmp]# grep 'info' test_grep 
    2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info
    [root@centos-36_2 tmp]# 
    [root@centos-36_2 tmp]# grep -i 'info' test_grep 
    2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info
    2021-07-29 12:33:44-abc.py-542-INFO:  ===> rcp_msg_id: 2
    2021-07-29 13:15:15-kln.py-478-INFO: Received NotifyREQ
    [root@centos-36_2 tmp]#  
    
    • 翻转查找,过滤不符合规则的行
    [root@centos-36_2 tmp]# grep -v "[0-9]" test_grep 
    The Redis server has been running, ignore this step.
    
    IsPrincipal: false
    CoreName: broad_Aux
    
    [root@centos-36_2 tmp]# 
    
    • 从文件中获取匹配规则进行过滤
    [root@centos-36_2 tmp]# cat grep_txt 
    INFO
    NOTIFY
    [root@centos-36_2 tmp]# grep -f grep_txt test_grep 
    2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info
    2021-07-29 12:33:44-abc.py-542-INFO:  ===> rcp_msg_id: 2
    2021-07-29 13:15:15-kln.py-478-INFO: Received NotifyREQ
    2021-07-29 13:15:55-tor.py-550-NOTIFY: RPD IRA-RSP CORE-ID TLVs: 
    [root@centos-36_2 tmp]# 
    
    • 输出匹配字符串
    [root@centos-36_2 tmp]# grep -E "([0-9]{1,3}\\.){3}[0-9]{1,3}$" test_grep 
    CoreIpAddress: 172.16.36.218
    [root@centos-36_2 tmp]# 
    [root@centos-36_2 tmp]# grep -oE "([0-9]{1,3}\\.){3}[0-9]{1,3}$" test_grep
     172.16.36.218
    
    • 对多个文件按匹配规则进行过滤
    [root@centos-36_2 tmp]# cp test_grep  test_grep_bak
    [root@centos-36_2 tmp]# 
    [root@centos-36_2 tmp]# grep -oE "([0-9]{1,3}\\.){3}[0-9]{1,3}$" test_grep test_grep_bak 
    test_grep:172.16.36.218
    test_grep_bak:172.16.36.218
    [root@centos-36_2 tmp]# 
    
    • 按匹配规则进行过滤显示m个后停止显示
    [root@centos-36_2 tmp]# grep -i "info" test_grep
    2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info
    2021-07-29 12:33:44-abc.py-542-INFO:  ===> rcp_msg_id: 2
    2021-07-29 13:15:15-kln.py-478-INFO: Received NotifyREQ
    [root@centos-36_2 tmp]# 
    [root@centos-36_2 tmp]# grep -i -m1 "info" test_grep
    2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info
    [root@centos-36_2 tmp]# 
    
    • 脚本中静默模式的使用
    [root@centos-36_2 tmp]# cat grep_q.sh 
    #!/bin/bash
    grep -q ERR ./test_grep
    exist_err=$?
    echo ${exist_err}
    if [[ ${exist_err} -eq 0 ]]; then 
            echo "has error log"
    else
            echo "no error log"
    fi
    [root@centos-36_2 tmp]# 
    [root@centos-36_2 tmp]# grep  ERR ./test_grep
    2021-07-29 15:18:15-orr.py-553-ERR: Not Get RPD IRA-RSP
    [root@centos-36_2 tmp]# 
    [root@centos-36_2 tmp]# ./grep_q.sh 
    0
    has error log
    [root@centos-36_2 tmp]# 
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值