每天一个linux命令8之grep高级篇
-a 以文本文件方式搜索
-c 计算找到的符合行的次数
-i 忽略大小写
-n 顺便输出行号
-v 反向选择,即找 没有搜索字符串的行
-E 大写 ,搜索内容串支持正则
2实例
$grep -n 'the' regular_express.txt
$grep -nv 'the' regular_express.txt
[] 表示其中的某一个字符 ,例如[ade] 表示a或d或e
$ grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! the soup taste good!
$ grep -n '[^g]oo' regular_express.txt
2:apple is my favorite food.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
19Goooooogle yes!
搜索包含数字的行
$ grep -n '[0-9]' regular_express.txt
5:However ,this dress is about $ 3183 dollars.
15:You are the best is menu you are the no.1.
这里^与[]里面使用的^意义不同。它表示^后面的串是在行的开头。
比如搜索the在开头的行
$ grep -n '^the' regular_express.txt
12:the symbol '*' is represented as star.
$ grep -n '^[a-z]' regular_express.txt
2:apple is my favorite food.4:this dress doesn't fit me.10:motorcycle is cheap than car.12:the symbol '*' is represented as star.18:google is the best tools for search keyword.19:goooooogle yes!20:go! go! Let's go.
搜索开头不是英文字母的行
$ grep -n '^[^a-zA-Z]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.21:#I am VBird
$表示它前面的串是在行的结尾,比如 '\.' 表示 . 在一行的结尾
搜索末尾是.的行
$ grep -n '\.$' regular_express.txt //. 是正则表达式的特殊符号,所以要用\转义
1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.4:this dress doesn't fit me.5:However ,this dress is about $ 3183 dollars.6:GNU is free air not free beer.
注意在MS的系统下生成的文本文件,换行会加上一个 ^M 字符。所以最后的字符会是隐藏的^M ,在处理Windows
下面的文本时要特别注意!
可以用cat dos_file | tr -d '\r' > unix_file 来删除^M符号。 ^M==\r
那么'^$' 就表示只有行首行尾的空行拉!
搜索空行
$ grep -n '^$' regular_express.txt
22:23:
搜索非空行
$ grep -vn '^$' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.4:this dress doesn't fit me.
任意一个字符. 与重复字符 *
在bash中*代表通配符,用来代表任意个字符,但是在正则表达式中,他含义不同,*表示有0个或多个 某个字符。
例如 oo*, 表示第一个o一定存在,第二个o可以有一个或多个,也可以没有,因此代表至少一个o.
点. 代表一个任意字符,必须存在。 g??d 可以用 'g..d' 表示。 good ,gxxd ,gabd .....都符合。
$ grep -n 'g..d' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.9:Oh! the soup taste good!16:The world is the same with 'glad'.
搜索两个o以上的字符串
$ grep -n 'ooo*' regular_express.txt //前两个o一定存在,第三个o可没有,也可有多个。
1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.9:Oh! the soup taste good!18:google is the best tools for search keyword.19:goooooogle yes!
搜索g开头和结尾,中间是至少一个o的字符串,即gog, goog....gooog...等
$ grep -n 'goo*g' regular_express.txt
18:google is the best tools for search keyword.19:goooooogle yes!
搜索g开头和结尾的字符串在的行
$ grep -n 'g.*g' regular_express.txt // .*表示 0个或多个任意字符
1:"Open Source" is a good mechanism to develop programs.14:The gd software is a library for drafting programs.18:google is the best tools for search keyword.19:goooooogle yes!20:go! go! Let's go.
限定连续重复字符的范围 { }
. * 只能限制0个或多个, 如果要确切的限制字符重复数量,就用{范围} 。范围是数字用,隔开 2,5 表示2~5个,
2表示2个,2, 表示2到更多个
注意,由于{ }在SHELL中有特殊意义,因此作为正则表达式用的时候要用\转义一下。
搜索包含两个o的字符串的行。
$ grep -n 'o\{2\}' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.9:Oh! the soup taste good!18:google is the best tools for search keyword.19:goooooogle yes!
搜索g后面跟2~5个o,后面再跟一个g的字符串的行。
$ grep -n 'go\{2,5\}g' regular_express.txt
18:google is the best tools for search keyword.
搜索包含g后面跟2个以上o,后面再跟g的行。。
$ grep -n 'go\{2,\}g' regular_express.txt
18:google is the best tools for search keyword.19:goooooogle yes!
注意,相让[]中的^ - 不表现特殊意义,可以放在[]里面内容的后面。
'[^a-z\.!^ -]' 表示没有小写字母,没有. 没有!, 没有空格,没有- 的 串,注意[]里面有个小空格。
另外shell 里面的反向选择为[!range], 正则里面是 [^range]
2扩展正则表达式
扩展正则表达式是对基础正则表达式添加了几个特殊构成的。
它令某些操作更加方便。
比如我们要去除 空白行和行首为 #的行, 会这样用:
$ grep -v '^$' regular_express.txt | grep -v '^#'
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
............
然而使用支持扩展正则表达式的 egrep 与扩展特殊符号 | ,会方便许多。
注意grep只支持基础表达式, 而egrep 支持扩展的,其实 egrep 是 grep -E 的别名而已。因此grep -E 支持扩展正则。
那么:
$ egrep -v '^$|^#' regular_express.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
....................
这里| 表示或的关系。 即满足 ^$ 或者 ^# 的字符串。
这里列出几个扩展特殊符号:
+,于 . * 作用类似,表示 一个或多个重复字符。?, 于 . * 作用类似,表示0个或一个字符。|,表示或关系,比如 'gd|good|dog' 表示有gd,good或dog的串(),将部分内容合成一个单元组。比如 要搜索 glad 或 good 可以这样 'g(la|oo)d'()的好处是可以对小组使用 + ? * 等。比如要搜索A和C开头结尾,中间有至少一个(xyz) 的串,可以这样 : 'A(xyz)+C'
每天一个linux命令8之grep高级篇的更多相关文章
- 【转载】每天一个Linux命令
目 录 每天一个linux命令(1) : ls 命令 每天一个linux命令(2) : cd 命令 每天一个linux命令(3) : pwd 命令 每天一个linux命令(4) : mkdi ...
- 每天一个 Linux 命令(21):find命令之xargs
在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行.但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出 ...
- 每天一个linux命令目录
出处:http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每 ...
- 每天一个 Linux 命令(20):find命令之exec
find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec 参数后面跟的是command ...
- 每天一个linux命令(20):find命令之exec
find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec 参数后面跟的是command ...
- 每天一个linux命令-转载
每天一个linux命令目录 转载自: http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令 ...
- [转]每天一个linux命令目录
[转]每天一个linux命令目录 http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令,坚持每 ...
- 每天一个Linux命令(20)--find命令之exec
find 是我们很常用的一个Linux命令,但是我们一般查找出来的额并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec 参数后面跟的是 comm ...
- 每天一个Linux命令 (转)
一. 文件目录操作命令: 1.每天一个linux命令(1):ls命令 2.每天一个linux命令(2):cd命令 3.每天一个linux命令(3):pwd命令 4.每天一个linux命令(4):mk ...
随机推荐
- CSU-2110 Keeping Cool
题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2110 题目 Description Kevin has just got ...
- initialization of 'zf' is skipped by 'case' label原因及解决方法
原因:switch 的 case 中不能定义变量,不然就会报错.可能是变量的初始化会因为有时候case条件不被执行而跳过. 解决方法: 1:在case中用{}将代码括起来,这样在{}中就能定义变量了; ...
- # ML学习小笔记—Where does the error come from?
关于本课程的相关资料http://speech.ee.ntu.edu.tw/~tlkagk/courses_ML17.html 错误来自哪里? error due to "bias" ...
- BZOJ 1208 [HNOI2004]宠物收养所 | SPlay模板题
题目: 洛谷也能评 题解: 记录一下当前树维护是宠物还是人,用Splay维护插入和删除. 对于任何一次询问操作都求一下value的前驱和后继(这里前驱和后继是可以和value相等的),比较哪个差值绝对 ...
- POJ 2942 Knights of the Round Table 补图+tarjan求点双联通分量+二分图染色+debug
题面还好,就不描述了 重点说题解: 由于仇恨关系不好处理,所以可以搞补图存不仇恨关系, 如果一个桌子上面的人能坐到一起,显然他们满足能构成一个环 所以跑点双联通分量 求点双联通分量我用的是向栈中pus ...
- BZOJ3668 [Noi2014]起床困难综合症 【贪心】
题目 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚持与起床困难综合症作斗争.通过研究相关文献,他找到了该病的发病原因 ...
- 【POJ 2387 Til the Cows Come Home】
Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 59755Accepted: 20336 Description Bessie is ...
- [经验分享]WeTouch中使用VueInputCode
如果你想在WeTouch中使用以下输入框的效果,需要做以下几个步骤: 第一步:安装Node.js 第二步:使用Npm在全局安装vue-input-code ,使用命令提示符,在Node安装根目录输入以 ...
- 【转】shell脚本写的俄罗斯方块游戏
亲测一个很好玩的shell脚本写的俄罗斯方块游戏,脚本来自互联网 先来讲一下思维流程 一.方块的表示 由于shell不能定义二维数组,所以只能用一维数组表示方块,俄罗斯方块主要可以分为7类,每一类方块 ...
- 今天做一个winform,想直接把窗体改成输出类库,其他地方直接调结果总提示不能注册组件,回来调度,可以,总结,windows还是直接用新建的类型项目,改容易出错
如题, 对于winform程序,还是新建一个类库,这样,在类库里面可以添加窗体.这样可以提供其他程序集来调用里面的窗体