一、简介

grep是一款强大的文本搜索工具,支持正则表达式。

语法

grep [option]... PATTERN [FILE]...

常用:

usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]][-e pattern] [-f file] [--binary-files=value] [--color=when][--context[=num]] [--directories=action] [--label] [--line-buffered][--null] [pattern] [file ...]

常用参数:

-v        取反
-i        忽略大小写
-c        符合条件的行数
-n        输出的同时打印行号
^*        以*开头
*$        以*结尾
^$        空行 

实际使用

1、准备一个txt文本monkey

[root@iz2ze76ybn73dvwmdij06zz ~]# cat monkey     #查看文本monkey
One day,a little monkey is playing by the well.一天,有只小猴子在井边玩儿.
He looks in the well and shouts :它往井里一瞧,高喊道:
“Oh!My god!The moon has fallen into the well!” “噢!我的天!月亮掉到井里头啦!”
An older monkeys runs over,takes a look,and says,一只大猴子跑来一看,说,
“Goodness me!The moon is really in the water!” “糟啦!月亮掉在井里头啦!”
And olderly monkey comes over.老猴子也跑过来.
He is very surprised as well and cries out:他也非常惊奇,喊道:
“The moon is in the well.” “糟了,月亮掉在井里头了!”
A group of monkeys run over to the well .一群猴子跑到井边来,
They look at the moon in the well and shout:他们看到井里的月亮,喊道:
“The moon did fall into the well!Come on!Let’get it out!”
“月亮掉在井里头啦!快来!让我们把它捞起来!”
Then,the oldest monkey hangs on the tree up side down ,with his feet on the branch .
然后,老猴子倒挂在大树上,
And he pulls the next monkey’s feet with his hands.拉住大猴子的脚,
All the other monkeys follow his suit,其他的猴子一个个跟着,
And they join each other one by one down to the moon in the well.
它们一只连着一只直到井里.
Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他们摸到月亮的时候,老猴子抬头发现月亮挂在天上呢
He yells excitedly “Don’t be so foolish!The moon is still in the sky!”
它兴奋地大叫:“别蠢了!月亮还好好地挂在天上呢!

直接查找符合条件的行

[root@iz2ze76ybn73dvwmdij06zz ~]# grep moon monkey     #从文本monkey中查找moon
“Oh!My god!The moon has fallen into the well!” “噢!我的天!月亮掉到井里头啦!”
“Goodness me!The moon is really in the water!” “糟啦!月亮掉在井里头啦!”
“The moon is in the well.” “糟了,月亮掉在井里头了!”
They look at the moon in the well and shout:他们看到井里的月亮,喊道:
“The moon did fall into the well!Come on!Let’get it out!”
And they join each other one by one down to the moon in the well.
Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他们摸到月亮的时候,老猴子抬头发现月亮挂在天上呢
He yells excitedly “Don’t be so foolish!The moon is still in the sky!”

查找反向符合条件的行

[root@iz2ze76ybn73dvwmdij06zz ~]# grep -v  moon monkey
One day,a little monkey is playing by the well.一天,有只小猴子在井边玩儿.
He looks in the well and shouts :它往井里一瞧,高喊道:
An older monkeys runs over,takes a look,and says,一只大猴子跑来一看,说,
And olderly monkey comes over.老猴子也跑过来.
He is very surprised as well and cries out:他也非常惊奇,喊道:
A group of monkeys run over to the well .一群猴子跑到井边来,
“月亮掉在井里头啦!快来!让我们把它捞起来!”
Then,the oldest monkey hangs on the tree up side down ,with his feet on the branch .
然后,老猴子倒挂在大树上,
And he pulls the next monkey’s feet with his hands.拉住大猴子的脚,
All the other monkeys follow his suit,其他的猴子一个个跟着,
它们一只连着一只直到井里.
它兴奋地大叫:“别蠢了!月亮还好好地挂在天上呢!”

直接查找符合条件的行数

[root@iz2ze76ybn73dvwmdij06zz ~]# grep -c  moon monkey
8

忽略大小写查找符合条件的行数

[root@iz2ze76ybn73dvwmdij06zz ~]# grep my monkey
[root@iz2ze76ybn73dvwmdij06zz ~]# grep -i my monkey
“Oh!My god!The moon has fallen into the well!” “噢!我的天!月亮掉到井里头啦!”

查找符合条件的行并输出行号

[root@iz2ze76ybn73dvwmdij06zz ~]# grep -n monkey monkey
1:One day,a little monkey is playing by the well.一天,有只小猴子在井边玩儿.
4:An older monkeys runs over,takes a look,and says,一只大猴子跑来一看,说,
6:And olderly monkey comes over.老猴子也跑过来.
9:A group of monkeys run over to the well .一群猴子跑到井边来,
13:Then,the oldest monkey hangs on the tree up side down ,with his feet on the branch .
15:And he pulls the next monkey’s feet with his hands.拉住大猴子的脚,
16:All the other monkeys follow his suit,其他的猴子一个个跟着,
19:Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他们摸到月亮的时候,老猴子抬头发现月亮挂在天上呢

查找开头是J的行

[root@iz2ze76ybn73dvwmdij06zz ~]# grep '^J' monkey
Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他们摸到月亮的时候,老猴子抬头发现月亮挂在天上呢

查找结尾是呢的行

[root@iz2ze76ybn73dvwmdij06zz ~]# grep "呢$" monkey
Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他们摸到月亮的时候,老猴子抬头发现月亮挂在天上呢

当然我们也可以使用命令

grep -help

查看更多相关的命令。

高效文本处理三剑客之一【grep】相关推荐

  1. linux文本三剑客,Linux文本处理三剑客之grep

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配到的行打印出来. grep的全称是:Global Search Regular Expression and ...

  2. linux 删除所有a字符串,linux文本处理三剑客(grep、sed、akw)命令选项整理

    摘要:Linux中最重要的三个命令:awk,sed,grep,在业界被称为"三剑客". 1.grep和egrep(过滤器) grep: grep [选项][匹配条件][file1 ...

  3. Linux中锚定符号的作用,Linux基础(9)文本处理三剑客之grep

    grep的作用:根据指定的模式搜索文本,将匹配到的文本行显示出来 语法格式:grep [options] "PATTERN" file PATTERN为正则表达式,grep一般是与 ...

  4. Linux基础(9)文本处理三剑客之grep

    grep的作用:根据指定的模式搜索文本,将匹配到的文本行显示出来 语法格式:grep [options] "PATTERN" file PATTERN为正则表达式,grep一般是与 ...

  5. 文本处理三剑客(grep sed wak)

    gerp(行处理) 重在查找 -a 或 --text : 不要忽略二进制的数据. -A<显示行数> 或 --after-context=<显示行数> : 除了显示符合范本样式的 ...

  6. linux 下 grep -c sh* /etc/passwd,Linux文本处理三剑客--grep

    稍微接触过linux都会知道有三个非常强大文本处理工具,那就是grep.sed和awk,想必都有听说过吧. Linux文本处理三剑客: grep, egrep, fgrep:文本过滤工具(模式:pat ...

  7. linux文本三剑客演讲稿,linux文本三剑客之grep

    grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...

  8. Linux文本处理三剑客之sed

    推荐新手阅读[酷壳]或[骏马金龙]开篇的教程作为入门.骏马兄后面的文章以及官方英文文档较难. [酷壳]:https://coolshell.cn/articles/9104.html [骏马金龙-博客 ...

  9. Linux三剑客之grep 与 egrep

    grep: Linux上文本处理三剑客 1 grep:文本过滤(模式:pattern)工具; *(grep, egrep, fgrep) 2 sed:stream editor,文本编辑工具: 3 a ...

最新文章

  1. java dozer 深度_java – Dozer深度映射设置为Set
  2. 理解SQLNET.AUTHENTICATION_SERVICES参数|转|
  3. 新手小白零基础搭建个人博客(三)部署篇
  4. 阿里云OSS Multipart Upload上传实例
  5. ArcGIS实验教程——实验十:矢量数据投影变换
  6. P1090-合并果子【离散化,队列,时间复杂度O(n)】
  7. 亚信安全发布“安全定义边界”2020发展理念 赋能企业在5G时代的数字化安全运营能力
  8. Django基础之中间件
  9. inside-the-linux-kernel-full
  10. javascript call与apply关键字的作用
  11. 用好Windows 7自带文件加密工具
  12. Java面试应该准备的知识点系列一
  13. erlang随机数问题
  14. caffe 安装教程(一)
  15. 打印机出现另存为xps_win7系统打印文件时弹出另存为xps/pdf的解决方法
  16. 目标检测 | 盘点提升小目标检测的思路
  17. CHB-MIT波士顿儿童医院癫痫EEG脑电数据处理(二)
  18. 浏览器input自动填充
  19. TensorFlow实现非线性回归
  20. GITHUB下载慢解决办法-插件解决

热门文章

  1. Apple Watch无法解锁Mac电脑
  2. 【评测】华硕灵耀X双屏2023笔记本电脑
  3. STM32进阶学习(1)-ESP01-S的AT指令测试、MQTT原理
  4. 搭载1亿像素相机,小米新品手机再成“性价比之王”
  5. 关于微信新出的企业付款到银行卡API的问题(二)
  6. mysql npe问题_万恶的 NPE 如何避免,几种你必须知道的方案!!!
  7. 百度网盘扩容两种技术讲解!真的有永久扩容吗?【粉丝福利】
  8. windows10 设置虚拟网卡/ip
  9. 实际物理内存16G,但可用的只有2G,远小于实际物理内存
  10. Docker 镜像部署服务器流程