Linux 文本处理工具 - grep(查找符合条件字符串)

Linux 文本处理工具 - grep(查找符合条件字符串)


简介

  • Linux grep 命令用于查找文件里符合条件的字符串。
  • grep 指令用于查找内容包含指定的范本样式的文件,如果发现某文件的内容符合所指定的范本样式,预设 grep 指令会把含有范本样式的那一列显示出来。若不指定任何文件名称,或是所给予的文件名为 -,则 grep 指令会从标准输入设备读取数据。
  • 参考文章: https://www.runoob.com/linux/linux-comm-grep.html
  • 官方文档: http://www.gnu.org/software/grep/manual/

常用参数

  • 常用参数说明
参数说明
-E相当于 ‘egrep’,启用扩展正则表达
-i忽略大小写
-v反向过滤
-数字显示过滤行及上面几行和下面几行
-n显示匹配的行所在行号
-A显示过滤行以及下面几行
-B显示过滤行以及上面几行

  • grep字符数量匹配规则
用法说明
^linux以linux开头
linux$以linux结尾
w…sw开头s结尾中间4个任意字符
…ss结尾前面5个任意字符
*字符出现任意
.*.为*
o*o为*
  • -E " " 中的字符匹配规则
用法说明
?0到1次
+1次到任意次
{n}n 次数
{m,n}m到n次
{0,n}0-n次
{,n}0-n次
{m,}最少m次
(eg){2}eg字符串出现2次

实例

  • 测试文本
[root@ grep]# cat > test << EOF
> root
> rootlinux
> linuxroot
> ROOT
> roooot
> rot
> egegeg
> egeg
> EOF
[root@ grep]#

  • 过滤含 " root " 的内容
[root@ grep]# grep root test  
root
rootlinux
linuxroot
[root@ grep]#

  • 过滤 以 " root " 开头的内容
[root@ grep]# grep ^root test 
root
rootlinux
[root@ grep]#

  • 过滤 以 " root " 结尾的内容
[root@ grep]# grep root$ test 
root
linuxroot
[root@ grep]#

  • 忽略大小写
[root@ grep]# grep -i root test 
root
rootlinux
linuxroot
ROOT
[root@ grep]#

  • " root " 字符之前不能有字符
[root@ grep]# grep -E "\<root" test  
root
rootlinux
[root@ grep]#

  • " root "字符之后不能有字符
[root@ grep]# grep -E "root\>" test  
root
linuxroot
[root@ grep]#

  • 显示过滤行以及上面一行和下面一行
[root@ grep]# grep rot test -1
roooot
rot
egegeg
[root@ grep]#

  • 显示匹配的行所在行号
[root@ grep]# grep rot  test -n
6:rot
[root@ grep]#

  • 显示过滤行以及下面两行
[root@ grep]# grep rot test -A 2
rot
egegeg
egeg
[root@ grep]#

  • 显示过滤行以及上面两行
[root@ grep]# grep rot test -B 2
ROOT
roooot
rot
[root@ grep]#

  • 反向过滤
[root@ grep]# grep -v root test
ROOT
roooot
rot
egegeg
egeg
[root@ grep]# 

  • " r " 开头 " t "结尾 中间4个任意字符
[root@ grep]# grep r....t test 
roooot
[root@ grep]#

  • " t " 结尾 前面4个任意字符
[root@ grep]# grep ....t test 
linuxroot
roooot
[root@ grep]#

  • " o " 为任意个
[root@ grep]# grep ro*  test  
root
rootlinux
linuxroot
roooot
rot
[root@ grep]#

  • " o " 为0到1次
[root@ grep]# grep -E "ro?t" test 
rot
[root@ grep]#

  • " o " 为1次到任意次
[root@ grep]# grep -E "ro+t" test 
root
rootlinux
linuxroot
roooot
rot

  • " o " 为 4 个
[root@ grep]# grep -E "ro{4}t" test  
roooot
[root@ grep]#

  • " o " 为2 到 4 个,包括 2,4个
[root@ grep]# grep -E "ro{2,4}t" test 
root
rootlinux
linuxroot
roooot
[root@ grep]#

  • " o " 为 0 到 2 个
[root@ grep]# grep -E "ro{0,2}t" test 
root
rootlinux
linuxroot
rot
[root@ grep]#

  • " o " 为 0 到 2 个
[root@ grep]# grep -E "ro{,2}t" test 
root
rootlinux
linuxroot
rot
[root@ grep]#

  • " o " 最少为 2 个
[root@ grep]# grep -E "ro{2,}t" test 
root
rootlinux
linuxroot
roooot
[root@ grep]#

  • " eg "字符串出现2次
[root@ grep]# grep -E "(eg){2}" test 
egegeg
egeg
[root@ grep]#



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值