<Linux> grep文件搜索
2015-01-10 15:22
441 查看
grep 是Global search Regular Expression的缩写。用于文件的字符串搜索。支持正则表达式。
使用方法:
grep [Options] Pattern FILE[s]
Options常用的有以下:
-c 只输出匹配行的计数。
-i 不区分大小写(只适用于单字符)。
-l 查询多文件时只输出包含匹配字符的文件名。
-n 显示匹配行及行号。
-v 显示不包含匹配文本的所有行。
-e 正则表达式
-w 以单词为单位匹配
-A 显示匹配行和之后的N行
-B 显示匹配行和之前的N行
-C 显示匹配行和之前之后的N行
Pattern:
可使用正则表达式
FILE[s]:
可单个文件,也可多个,也可以是正则表达式的文件。
示例:
测试文本test.txt:
This is the first string.
THIS IS THE SECOND STRING.
third line..
the end;
This is the first string.
This is the first string.
THIS IS THE SECOND STRING.
1:This is the first string.
2:THIS IS THE SECOND STRING.
2
This is the first string.
$grep "is" test.txt
This
is the first string.
// 显示匹配之后的2行
This is the first string.
THIS IS THE SECOND STRING.
third line..
$ grep -B 2 -w "end" test.txt // 显示匹配之前的2行
THIS IS THE SECOND STRING.
third line..
the end;
$grep -C 2 -w "IS" test.txt
// 显示匹配前后的2行,test.txt总共4行
This is the first string.
THIS IS THE SECOND STRING.
third line..
the end;
3:third line..
4:the end;
3:third line..
dll.c:#include <stdio.h>
gbtest.c:#include <stdio.h>
redir.txt:#include <stdio.h>
test.c:#include <stdio.h>
dll.c
gbtest.c
test.c
This is the first string.
使用方法:
grep [Options] Pattern FILE[s]
Options常用的有以下:
-c 只输出匹配行的计数。
-i 不区分大小写(只适用于单字符)。
-l 查询多文件时只输出包含匹配字符的文件名。
-n 显示匹配行及行号。
-v 显示不包含匹配文本的所有行。
-e 正则表达式
-w 以单词为单位匹配
-A 显示匹配行和之后的N行
-B 显示匹配行和之前的N行
-C 显示匹配行和之前之后的N行
Pattern:
可使用正则表达式
FILE[s]:
可单个文件,也可多个,也可以是正则表达式的文件。
示例:
测试文本test.txt:
This is the first string.
THIS IS THE SECOND STRING.
third line..
the end;
普通搜索:
$ grep "string" test.txtThis is the first string.
不区分大小写:
$ grep -i "string" test.txtThis is the first string.
THIS IS THE SECOND STRING.
显示行号:
$ grep -i -n "string" test.txt1:This is the first string.
2:THIS IS THE SECOND STRING.
只显示匹配的行号:
$ grep -i -c "string" test.txt2
以单词为单位搜索:
$ grep -w "is" test.txtThis is the first string.
$grep "is" test.txt
This
is the first string.
显示匹配前后的N行:
$ grep -A 2 -w "is" test.txt// 显示匹配之后的2行
This is the first string.
THIS IS THE SECOND STRING.
third line..
$ grep -B 2 -w "end" test.txt // 显示匹配之前的2行
THIS IS THE SECOND STRING.
third line..
the end;
$grep -C 2 -w "IS" test.txt
// 显示匹配前后的2行,test.txt总共4行
This is the first string.
THIS IS THE SECOND STRING.
third line..
the end;
反搜索,显示不包括字符串的行
$ grep -n -i -v "this" test.txt3:third line..
4:the end;
多个不匹配的条件:
$ grep -n -i -v -e "this" -e "the" test.txt3:third line..
搜索多个文件:
$ grep "include" * // 当前目录下所有含有"include"的文件及匹配行, 加参数-l 只显示文件名dll.c:#include <stdio.h>
gbtest.c:#include <stdio.h>
redir.txt:#include <stdio.h>
test.c:#include <stdio.h>
|管道作为输入:
$ ls | grep ".c" 搜索当前文件夹下.c文件dll.c
gbtest.c
test.c
This is the first string.
相关文章推荐
- <<探讨linux/unix>>之文件类型
- <转>linux文件合并,去重
- <转>linux文件读写浅析
- &lt;org manual&gt;翻译--4.7 文件链接中的搜索选项
- <转载>在Linux下文件的三种时间标记:访问时间、修改时间、状态改动时间
- <<探讨linux/unix>>之命令ls
- <cstring>头文件
- <转载>Win7文件关联 文件与程序“联姻”
- <转>C语言中的文件输入输出函数
- linux下查找当前目录下,所以包含&quot;&lt;s3c2410.h&gt;&quot;的普通文件
- <Linux笔记>Arch Linux小结 硬盘安装
- <<linux下无线上网>>
- 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 <configuration>
- <cmath>头文件
- <ctype>头文件
- <memo><linux>log的保存(添加中)
- <转>linux虚拟文件系统浅析
- 首页 > Ubuntu 命令 > linux 查看文件内容
- <string>和<string.h>两头文件区别
- <<探讨linux/unix>>之命令find