linux grep命令操作指南

本文深入解析grep命令的功能、作用、格式及使用实例,包括查找字符串数据、大小写透明、输出行号、颜色标记等功能,并通过具体示例演示如何在实际场景中应用grep命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

          ① 定义
         
             grep是行级设备,它分析一行信息,若当中有我们所需要的信息,就将整行返回
            
          ② 作用
         
             进行字符串数据的对比,将符合用户需求的字符串打印出来
            
          ③ 格式
         
             grep [-acinv] [--color=auto] '查找字符串' filename
            
             参数:
             -a:将二进制文件一text文件的方式查找数据
             -c:计算找到‘查找字符串’的次数
             -i:对大小写透明
             -n:输出行号
             -v:显示没有‘查找字符串’内容的那一行
             -A:后面可加数字,为after的意思,除了列出该行外,后续的n行也列出来
             -B:后面可加数字,为before的意思,除了列出该行外,前面的n行也列出来
             --color=auto:关键字上色

          ④ 例子
             eg01:找出有检查点事件发生的标志

[oracle@localhost bdump]$ grep -in --color=auto 'checkpoint' alert_orcl.log
64:  checkpoint is 446074
66:  checkpoint is 446074
69:  checkpoint is 446074
72:  checkpoint is 446074
383:  checkpoint is 460200
1845:Checkpoint not complete
2112:  checkpoint is 606217
2114:  checkpoint is 606217
2116:  checkpoint is 606217
2118:  checkpoint is 606217
2120:  checkpoint is 606217


            eg02:从内核中找出网卡相关信息

[oracle@localhost bdump]$ dmesg | grep -n -A3 -B2 --color=auto 'eth'
599-iscsi: registered transport (iser)
600-iscsi: registered transport (be2iscsi)
601:eth0: no IPv6 routers present
602-Bluetooth: Core ver 2.10
603-NET: Registered protocol family 31
604-Bluetooth: HCI device and connection manager initialized
--
615-ip_conntrack version 2.4 (8192 buckets, 65536 max) - 228 bytes per conntrack
616-ip6_tables: (C) 2000-2006 Netfilter Core Team
617:peth0: Promiscuous mode enabled.
618:device peth0 entered promiscuous mode
619:xenbr0: port 1(peth0) entering forwarding state
620-device vif0.0 entered promiscuous mode
621-New device vif0.0 does not support netpoll
622-Disabling netpoll for xenbr0
--
625-hdc: drive_cmd: error=0x04 { AbortedCommand }
626-ide: failed opcode was: 0xec
627:eth0: no IPv6 routers present
628:xenbr0: port 1(peth0) entering disabled state
629:xenbr0: port 1(peth0) entering forwarding state


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值