博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux grep命令详解
阅读量:4042 次
发布时间:2019-05-24

本文共 1088 字,大约阅读时间需要 3 分钟。

14、grep抓取文件内容中特定字符

语法:grep <-option> Keywords filename/ filedirectory

filename/ filedirectory:文件名或文件所在的绝对路径。
要匹配的关键字Keywords可以加引号,也可以不加引号。

参数:

  • -E : 扩展的grep,支持复杂正则
  • -w : 加上单词的界定,也就是说只有完全匹配给定的单词的行才能被抓取出来
  • -F : 完全关闭正则
  • -v : 显示不匹配的项
  • -i : 不区分大小写
  • -o : 只显示匹配内容(模式匹配,给定什么显示什么)
  • -r : 递归参数(会递归遍历目录下的所有文件,包括二级目录、三级目录等下所有的文件)
  • -A : 搜出关键字以后,还可以向下打印指定的行数,后面接要打印的行数
  • -B :搜出关键字以后,还可以向上打印指定的行数,后面接要打印的行数

1).在a.txt文件中搜包含name的行打印出来

[root@admin t2]# grep name a.txtname is aa

2).抽取/root/install.log文件中匹配man关键字的内容

[root@admin /]# cat /root/install.log | grep -w manInstalling man-1.6f-32.el6.x86_64Installing man-pages-overrides-6.6.3-2.el6.noarchInstalling man-pages-3.22-20.el6.noarch

3).当只知道某个关键字,比如man,但不知道在哪个文件中定义的时候,可以使用grep递归的搜索

[root@admin /]# grep -r man *

上面命令中的星号’ *’ 代表的是通配当前面目录下的所有文件,也可以在具体某一个文件中搜索,如a.txt

4).使用-o进行模式匹配抽取

[root@admin tt]# grep -o 'this is' 11.txtthis isthis isthis is

5).匹配到关键name之以后,再向下打印n = 3行

[root@admin t2]# cat a.txt | grep -A 3 namenamecfwesdf12edf342sdfs

6).匹配到关键name之以后,再向上打印n = 3行

[root@admin t2]# cat a.txt | grep -B 3 nameadfasd   this is the last one %234123 ^adsftgrtname

转载地址:http://xbmdi.baihongyu.com/

你可能感兴趣的文章
adb command not found
查看>>
Xcode 启动页面禁用和显示
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Clone Graph(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
java自定义容器排序的两种方法
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
AngularJS2中最基本的文件说明
查看>>
从头开始学习jsp(2)——jsp的基本语法
查看>>
使用与或运算完成两个整数的相加
查看>>
备忘:java中的递归
查看>>
DIV/CSS:一个贴在左上角的标签
查看>>
Solr及Spring-Data-Solr入门学习
查看>>