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获取及比较线…

LuoGu P2002 消息扩散

题目传送门 这个题其实就是tarjan缩点的板子题对吧....至少我是这么想的 首先这是个有向图&#xff0c;对于一个有向图&#xff0c;我们肯定要考虑环的存在与否&#xff0c;恰好这个题又是让我们找出最少的点&#xff0c;使得这几个点能够走遍全图 那么&#xff0c;显然&#x…

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;所以我们很轻易就能够买得起这样的产品。而在手机、电脑普及之后&#…

邮件系统磁盘监控脚本

#!/bin/shecho "邮件系统磁盘每周检测情况" >> /var/wangyang/checkdisk.txtuse"38.4G"var"819.2G"boot"160M"wang"/var/wangyang"cd $wangdf -h > /var/wangyang/disk.txt####根分区取值######warncat disk.txt |…

安卓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…

常用python分析数据包pipinstallnumpy_安装numpy和matplotlib时,pip依赖关系解析失败

从一个干净的python2.7.3安装开始&#xff0c;如果我运行pip install -r requirements.txt包括以下内容要求.txt公司名称&#xff1a;^{pr2}$然后pip尝试在numpy之前安装matplotlib&#xff0c;但失败的原因是&#xff1a;BUILDING MATPLOTLIBmatplotlib: 1.2.1python: 2.7.3 (…

java socket 判断Socket连接失效

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

MySQL存储过程权限检查主要点

一、权限相关&#xff1a;1.修改sql security Sql代码 ALTER PROCEDURE www SQL SECURITY INVOKER &#xff1b; ALTER PROCEDURE www SQL SECURITY DEFINER &#xff1b; &#xff08;1&#xff09;MySQL存储过程是通过指定SQL SECURITY子句指定执行存储过程的实际用户&…

安卓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生命周期函数一…

BZOJ 1016--[JSOI2008]最小生成树计数(kruskal搜索)

1016: [JSOI2008]最小生成树计数 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 7429 Solved: 3098[Submit][Status][Discuss]Description 现在给出了一个简单无向加权图。你不满足于求出这个图的最小生成树&#xff0c;而希望知道这个图中有多少个不同的最小生成树。&…

python until语句_详解Lua中repeat...until循环语句的使用方法

与for和while循环不同&#xff0c;在循环的顶部测试循环条件&#xff0c;Lua编程语言的repeat...until 循环检查循环底部的状态。repeat...until 循环类似于while循环&#xff0c;不同的是do ... while循环是保证至少执行一次。语法Lua编程语言repeat...until循环的语法是&…

Javascript s08

AJAX ------------------------- AJAX 指异步 JavaScript 及 XML&#xff08;Asynchronous JavaScript And XML&#xff09;。 举例! 异步是发短信,同步是打电话. 异步的时候,会有回调. 想用JS去服务器拿数据? 客户端的JS.所以没有能力去服务器拿信息 借用服务端的PHP,JS模拟一…

linux驱动开发---并发控制

中断屏蔽 local_irq_disable();/* 临界代码*/local_irq_enable();2.原子操作 整形原子操作&#xff1a;void atomic_set(atomix_t *v,int i) //设置原子变量值为iatomic_t v ATOMIC_INIT(2); //设原子变量v值为2atomic_read(atomic_t *v);void atomic_add(int i,atomic_t *…

安卓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…

Python基础—06-函数基础

函数基础 函数简介 定义&#xff1a;就是具有特定功能的一段代码优点&#xff1a; 解决代码的重复书写可以将功能的实现着和使用者分开&#xff0c;提高开发效率分类&#xff1a; 库函数&#xff1a;print、input、abs等自定义&#xff1a;用户自己封装的函数函数使用 定义函数…