当前位置: 移动技术网 > 科技>操作系统>Linux > Linux文本检索命令grep笔记

Linux文本检索命令grep笔记

2019年05月28日  | 移动技术网科技  | 我要评论

  grep是在linux系统中基于行文本非常实用检索工具,通过该命令可以将匹配到的结果信息输出到终端控制台。

  语法格式:grep [-ivnc] '需要匹配的内容' 文件名

  常用参数说明:

  -i 检索的时候不区分大小写

  -c 检索到的匹配行数

  -n 检索到的匹配行显式具体的行号

  -v 逆向匹配也就是不包含匹配项的结果检索出来

  准备工作:

  创建一个演示的测试文本文件 test.txt

  vim test.txt

  插入如下内容:

  today is saturday

  tommow is sumday

  my name is xiaoming

  today

  命令示例:

  grep 'today' test.txt #找到test.txt文件包含today的行

  输出结果:today is saturday

  grep -i 'today' test.txt #找到test.txt文件包含today的行不区分大小写

  输出结果:

  today is saturday

  today无锡男科医院哪家好 http://www.zzch120.com/

  grep -in 'today' test.txt

  #找到test.txt文件包含today的行不区分大小写包含行号

  输出结果:

  1:today is saturday

  4:today

  grep -ci 'today' test.txt #找到test.txt文件包含today的行书不区分大小写

  输出结果:2

  grep -vn 'today' test.txt #到test.txt文件不包含today的行书显式行号

  输出结果:

  tommow is sumday

  my name is xiaoming

  注意: cat 命令+管道符号进行改写

  比如 :

  grep -ni 'today' test.txt

  可以改写为:

  cat test.txt | grep -ni 'today'

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网