15个实用的grep示例

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

首先,新建如下文件demo_file

THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.Two lines above this line is empty.
And this is the last line.

1 在单个文件中搜索指定字符串

语法:grep "literal_string" filename

$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

2 在多个文件中搜索指定字符串

语法:grep "string" FILE_PATTERN

$cp demo_file demo_file1
$grep "this" demo_*#结果如下
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.
3 用grep -i忽略大小写

语法:grep -i "string" FILE

$grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.
4 匹配正则表达式

语法:grep "REGEX" filename

$ grep "lines.*empty" demo_file
Two lines above this line is empty.

补充一点正则:

?    前面的字段出现0或1次

*    前面的字段出现0或多次

+    前面的字段出现1或多次

{n}  前面的字段出现n次

{n,}  前面的字段出现n或者更多次

{,m}  前面的字段最多出现m次

{n,m} 前面的字段至少出现n次,最多出现m次

5 完整匹配,忽略字串

语法:grep -w "word" FILENAME

$grep -i "is" demo_file
#除匹配is之外,还会匹配his this等
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.
为防止这个现象,用-w

$ grep -iw "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
6 查看匹配行的前、后和周围(after/behind/around)

先新建demo_text

4. Vim Word NavigationYou may want to do several navigation in relation to the words, such as:* e - go to the end of the current word.* E - go to the end of the current WORD.* b - go to the previous (before) word.* B - go to the previous (before) WORD.* w - go to the next word.* W - go to the next WORD.WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.Example to show the difference between WORD and word* 192.168.1.1 - single WORD* 192.168.1.1 - seven words.
6.1 显示后N行

语法:grep -A <N> "string" FILENAME

$ grep -A 3 -i "example" demo_text
Example to show the difference between WORD and word* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
6.2 显示前N行

语法:grep -B <N> "string" FILENAME

$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word* 192.168.1.1 - single WORD
6.3 显示周围的行

直接上例子

$ grep -C 2 "Example" demo_text
word - word consists of a sequence of letters, digits and underscores.Example to show the difference between WORD and word* 192.168.1.1 - single WORD
7 高亮显示

export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
搜索关键字,匹配之后会显示绿色。

如果不想高亮显示,把auto改成none即可。

8 递归查找

语法:grep -r "string" .

会遍历指定目录下所有文件及子目录下所有文件。

9 用-v显示不匹配的行

语法:grep -v "string" FILENAME

下面的例子会匹配所有不包含go的行。

$grep -v "go" demo_txt
4. Vim Word NavigationYou may want to do several navigation in relation to the words, such as:WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.Example to show the difference between WORD and word* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
10 显示不匹配所有规则的行

$ cat test-file.txt
a
b
c
d$ grep -v -e "a" -e "b" -e "c" test-file.txt
d
11 计算匹配行数

语法:grep -c "pattern" filename

和-v连用就是计算不匹配的行数

12 只显示匹配的文件名

语法:grep -l "pattern" file*

注意:-l是小写的L

$ grep -l this demo_*
demo_file
demo_file1
13 只显示匹配的字符串(使用正则的时候特别有用)

$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line
14 显示匹配的位置

$ cat temp-file.txt
12345
12345$ grep -o -b "3" temp-file.txt
2:3
8:3
15 显示行号

$ grep -n "go" demo_text
5: * e - go to the end of the current word.
6: * E - go to the end of the current WORD.
7: * b - go to the previous (before) word.
8: * B - go to the previous (before) WORD.
9: * w - go to the next word.
10: * W - go to the next WORD.

转载于:https://my.oschina.net/csensix/blog/134846

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/266001.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IntelliJ IDEA 连接数据库 详细过程

IntelliJ IDEA集成了众多插件&#xff0c;方便开发者使用&#xff0c;使用其自带的Database模块就可以很方便的配置、连接到数据库&#xff0c;本次操作以MySQL为例&#xff0c;其中testjdbc数据库已经提前建好&#xff0c;里面有两张表emp_table 和 t_user&#xff0c;相关信息…

Linux线程编程

参考&#xff1a;Linux多线程编程初探 作者&#xff1a;峰子_仰望阳光 网址&#xff1a;https://www.cnblogs.com/xiehongfeng100/p/4620852.html 目录线程概述线程概念线程与进程区别为何用线程线程开发api概要线程创建、等待、退出线程创建线程退出线程等待线程ID获取及比较线…

Controlling Execution

2019独角兽企业重金招聘Python工程师标准>>> 逗号操作符 逗号操作符在java中特殊之处在于两个地方&#xff1a; 1. 定义同种类型的变量时可以这样写&#xff1a; int i10,j20;//只有同种变量才行 2. 在for循环中的初始化条件和变化的步骤中对于同种类型的可以连接…

前端 js 非控件 使用标签打印机 打印二维码和文本_青岛Web前端(HTML5)面试题分享...

HTML5是前端开发人员必须掌握的技能之一&#xff0c;那么在面试时&#xff0c;面试官常问到的HTML5的问题有哪些呢&#xff1f;青岛HTML51、Doctype作用&#xff1f;严格模式与混杂模式如何区分&#xff1f;它们有何意义&#xff1f;答&#xff1a;告知浏览器的解析器&#xff…

SpringBoot整合SpringBatch实用简例

SpringBatch主要是一个轻量级的大数据量的并行处理(批处理)的框架。 作用和Hadoop很相似&#xff0c;不过Hadoop是基于重量级的分布式环境(处理巨量数据)&#xff0c;而SpringBatch是基于轻量的应用框架(处理中小数据)。 这里使用SpringBatch做了一个能跑的最简单例子&#xff…

安卓APP_ Fragment(1)—— Fragment概念、基础用法、动态变换、管理栈

摘自&#xff1a;安卓APP_ Fragment&#xff08;1&#xff09;—— Fragment概念、基础用法、动态变换、管理栈 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-04-15 23:32:31 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/115716359 目录…

谷歌五笔输入法电脑版_“五笔输入法”打字速度更快,为什么却没啥人用?

现如今&#xff0c;随着国内经济水平的不断发展&#xff0c;电脑和手机都已经不再是稀罕玩意&#xff0c;因为现在国内有很多的厂家都在生产手机和电脑&#xff0c;导致这些电子产品的价格下降&#xff0c;所以我们很轻易就能够买得起这样的产品。而在手机、电脑普及之后&#…

安卓APP_ Fragment(2)—— Activity与Fragment的通信

摘自&#xff1a;安卓APP_ Fragment&#xff08;2&#xff09;—— Activity与Fragment的通信 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-04-16 17:23:44 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/115743009 目录 Activity与Fragm…

java socket 判断Socket连接失效

要判断socket连接链路是否可用时&#xff0c;不能通过socket.isClosed() 和 socket.isConnected() 方法判断&#xff0c;要通过心跳包 socket.sendUrgentData(0xFF) 。 当第一次连接成功后&#xff0c; socket.isClosed() false, socket.isConnected()true&#xff0c;只有在自…

安卓APP_ Fragment(3)—— Fragment的生命周期

摘自&#xff1a;安卓APP_ Fragment&#xff08;3&#xff09;—— Fragment的生命周期 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-04-16 22:32:12 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/115768202 目录 Fragment生命周期函数一…

安卓APP_ 布局(8) —— 基于 RecyclerView 的 ViewPager2翻页

摘自&#xff1a;安卓APP_ 布局&#xff08;8) —— 基于 RecyclerView 的 ViewPager2翻页 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-04-20 15:37:54 网址&#xff1a;https://blog.csdn.net/weixin_44742824/article/details/115803077 viewpager2 是对Recycler…

安卓APP_ Fragment(4)—— Fragment + ViewPager2 模拟微信首页 (1)两者联动实现翻页

摘自&#xff1a;安卓APP_ Fragment&#xff08;4&#xff09;—— Fragment ViewPager2 模拟微信首页 &#xff08;1&#xff09;两者联动实现翻页 作者&#xff1a;丶PURSUING 发布时间&#xff1a; 2021-04-20 17:46:59 网址&#xff1a;https://blog.csdn.net/weixin_4474…

Linux网络编程(Socket)

目录网络编程&#xff08;Socket&#xff09;概述引入网络编程通识扫盲socket套接字套接字描述符字节序socket编程步骤Linux提供的API简析创建套接字即连接协议[socket]&#xff08;服、客&#xff09;绑定IP和端口[bind]&#xff08;服&#xff09;地址转换api字节序转换api监…

嵌入式开发概述(树莓派介绍)

目录嵌入式定义嵌入式芯片选型ARM架构ARM树莓派嵌入式定义 国内普遍认同的嵌入式系统的定义是以应用为中心&#xff0c;以计算机技术为基础&#xff0c;软硬件可裁剪&#xff0c;适应应用系统对功耗、可靠性、成本、体积、功耗等严格要求的专用计算机系统。 嵌入式系统是软件和…

ubuntu 16gcc g++版本降级

打算在ubuntu16上编译linux3.4.66内核&#xff0c;但是遇到 In file included from include/linux/compiler.h:48:0, from include/linux/stddef.h:4, from include/linux/posix_types.h:4, from include/linux/types.h:17, from include/linux/page-flags.h:8, from kern…

android 系统gpu 调试_【资讯】高通公布首批可OTA更新GPU驱动手机:谷歌Pixel 4/三星S10在列...

文章转载自&#xff1a;iT之家原文链接&#xff1a;https://www.ithome.com/0/479/483.htm(IT之家3月25日消息) 谷歌今天宣布了一系列面向游戏开发者的新工具&#xff1a;Google Play Asset Delivery、Android性能调节器&#xff0c;Android GPU检查器和Cloud Firestore等&…

cURL库

一、cURL库可以做什么 1.cURL是一个文件传输工具&#xff0c;支持很多协议 二、cURL库的用法 1.初始化 $urlcurl_init() 2.设置选项 curl_setopt($url,CURLOPT_URL&#xff0c;"http://www.baidu.com") curl_setopt($url,string option,mixed value) 3.执行cURL会话 …

树莓派刷机

目录准备刷机重刷准备 SD卡&#xff08;一般16G以上&#xff0c;这里我用的8G&#xff09; 读卡器 Win32DiskImager&#xff08;将镜像烧到SD卡的软件&#xff09;&#xff0c;在这里下载&#xff1a;https://download.csdn.net/download/zhuguanlin121/18329615?spm1001.201…

linux3.4.2移植总结(s3c2440)

环境&#xff1a;Linux version 3.5.0-23-generic (builddkomainu) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) ubuntu12 目标板&#xff1a;JZ2440 待移植内核&#xff1a;3.4.2 交叉编译器&#xff1a;arm-linux-gcc-4.3.2 1. make s3c2410_defconfig //使…

判断图有无环_浅谈什么是图拓扑排序

1 引言 在工程实践中,一个工程项目往往由若干个子项目组成。这些子项目间往往有两种关系:  (1) 先后关系&#xff0c;即必须在某个项完成后才能开始实施另一个子项目。  (2) 子项目间无关系&#xff0c;即两个子项目可以同时进行,互不影响。例如&#xff1a;在工厂里产品的…