《Linux Shell》笔记之grep
来源:互联网 发布:视频会议录播软件 编辑:程序博客网 时间:2024/05/22 14:48
grep - 匹配文本行工具。对匹配行进行搜索,并打印出来.
grep(GlobelSearch Regular Expression and Printing out the line)
Unix的grep家族包括grep、egrep和fgrep
- egrep表示扩展的grep,相比grep支持更多的元字符,"grep -E"相当于egrep
- fgrep是fast grep,不支持元字符,但是搜索速度更快。grep搜索的结果被送到屏幕,不影响原文件内容。
- grep 语法:
grep [options] 'pattern' FILE
命令 选项 模式 文件
grep不加引号直接过滤字符串;
grep在进行模式匹配的时候必须加引号,单引和双引号都可以;
grep在引用变量的时候必须加双引号
- grep的选项[option]
常用选项
-r:递归的搜索 见例3.1
-v:反向选取,只显示不符合模式的行 见例3.2
-o:只显示被模式匹配到的字符串,而不是整个行 见例3.3
-i:匹配时不区分大小写 见例3.4
-A #:显示匹配到的行时,顺便显示后面的#行(#表示数值) 见例3.5
-B #:前面的#行 见例3.6
-C #:前后的#行 见例3.7
-E:使用扩展的正则表达式 见例3.8
-a 以文本文件方式搜索 见例3.9
-c 计算找到的符合行的次数 见例3.x1
-n 顺便输出行号
- 实例
新建文本example.grep,文本内容如下:
test@sha>cat example.grep
hello@linux@linux
this is linuxbash world
Do you likeit ?
GOODBYE linux
- 实例1 搜索包含关键字linux 所有文本行
test@sha>grep -r linux example.grep
hello@linux@linux
this is linuxbash world
GOODBYE linux
- 实例2 搜索不包含关键字linux 所有文本行
test@sha>grep -v linux example.grep
Do you likeit ?
- 实例3 搜索匹配关键字linux的字符串,不显示对应的整行
test@sha>grep -o linux example.grep
linux
linux
linux
linux
- 实例4 搜索包含关键字goodbye的行,不区分大小写
test@sha>grep -i goodbye example.grep
GOODBYE linux
- 实例5 搜索关键字like, 显示匹配行和后面的一行
test@sha>grep -A 1 like example.grep
Do you likeit ?
GOODBYE linux
- 实例6 搜索关键字like,显示匹配行和前面一行
test@sha>grep -B 1 like example.grep
this is linuxbash world
Do you likeit ?
- 实例7 搜索关键字like, 显示匹配行,前后各一行
test@sha>grep -C 1 like example.grep
this is linuxbash world
Do you likeit ?
GOODBYE linux
- 实例8 开启扩展正则表达式,搜索关键字linux出现过1,2次的行
test@sha>grep -E 'linux{1,2}' example.grep
hello@linux@linux
this is linuxbash world
GOODBYE linux
- 实例9 以文本文件方式,搜索abc
test@sha>grep -a linux example.grep
hello@linux@linux
this is linuxbash world
GOODBYE linux
- 实例10 计算出现linux的次数
test@sha:~/tmp>grep -c linux example.grep
- 实例11 输出行号
test@sha>grep -n linux example.grep
1:hello@linux@linux
2:this islinux bash world
4:GOODBYElinux
参考:
http://www.jb51.net/LINUXjishu/106140.html linux文本处理工具之一grep命令详解
http://blog.csdn.net/hellochenlian/article/details/34088179 grep用法详解:grep与正则表达式
- 《Linux Shell》笔记之grep
- linux之shell学习:grep
- linux之shell之grep,awk,sed
- Linux shell命令之wc和grep
- Linux之常用Shell命令:grep,find
- linux shell 之 grep简单使用
- linux-shell脚本命令之grep
- Linux shell编程之grep命令
- 《Linux Shell》笔记之grep与正则表达式(实例总结) 未完待续
- Linux Shell 命令--grep
- Linux Shell编程-grep
- Linux shell-grep
- shell编程之grep
- shell命令之grep
- shell 之 grep 指令
- linux学习笔记3之----grep用法
- Linux命令学习笔记之grep
- linux-shell(8) grep 命令的使用【自己的笔记】
- 时间空间复杂度
- asp.net之cookie(设置css)
- 协同过滤
- linux shell script 循环语句
- 【10/26】 iOS开发成长之路,【从零开始】
- 《Linux Shell》笔记之grep
- Android添加顶部通知
- Android Activity 启动模式
- error exception runtimeexception
- 《Linux Shell》笔记之正则表达式
- nodeJs get post
- 进击的KFC第七节:C语言:结构体篇
- IIS6切换PHP版本
- 图像缩放