grep是强大的文本搜索器。
grep --help 可以看各个参数的含义
用法: grep [选项]... 模式 [文件]...
在每个<文件>中查找给定<模式>。
例如:grep -i 'hello world' menu.h main.c
<模式>可以包括多个模式字符串,使用换行符进行分隔。
模式选择与解释:
-E, --extended-regexp <模式> 是扩展正则表达式
-F, --fixed-strings <模式> 是字符串
-G, --basic-regexp <模式> 是基本正则表达式
-P, --perl-regexp <模式> 是 Perl 正则表达式
-e, --regexp=<模式> 用指定的<模式>字符串来进行匹配操作
-f, --file=<文件> 从给定<文件>中取得<模式>
-i, --ignore-case 在模式和数据中忽略大小写
--no-ignore-case 不要忽略大小写(默认)
-w, --word-regexp 强制<模式>仅完全匹配字词
-x, --line-regexp 强制<模式>仅完全匹配整行
-z, --null-data 数据行以一个 0 字节结束,而非换行符
杂项:
-s, --no-messages 不显示错误信息
-v, --invert-match 选中不匹配的行
-V, --version 显示版本信息并退出
--help 显示此帮助并退出
输出控制:
-m, --max-count=<次数> 得到给定<次数>次匹配后停止
-b, --byte-offset 输出的同时打印字节偏移
-n, --line-number 输出的同时打印行号
--line-buffered 每行输出后刷新输出缓冲区
-H, --with-filename 为输出行打印文件名
-h, --no-filename 输出时不显示文件名前缀
--label=<标签> 将给定<标签>作为标准输入文件名前缀
-o, --only-matching 只显示行中非空匹配部分
-q, --quiet, --silent 不显示所有常规输出
--binary-files=TYPE 设定二进制文件的 TYPE(类型);
TYPE 可以是 'binary'、'text' 或 'without-match'
-a, --text 等同于 --binary-files=text
-I 等同于 --binary-files=without-match
-d, --directories=ACTION 读取目录的方式;
ACTION 可以是`read', `recurse',或`skip'
-D, --devices=ACTION 读取设备、先入先出队列、套接字的方式;
ACTION 可以是`read'或`skip'
-r, --recursive 等同于--directories=recurse
-R, --dereference-recursive 同上,但遍历所有符号链接
--include=GLOB 只查找匹配 GLOB(文件模式)的文件
--exclude=GLOB 跳过匹配 GLOB 的文件
--exclude-from=FILE 跳过所有匹配给定文件内容中任意模式的文件
--exclude-dir=GLOB 跳过所有匹配 GLOB 的目录
-L, --files-without-match 只打印没有匹配上的<文件>的名称
-l, --files-with-matches 只打印有匹配的<文件>的名称
-c, --count 只打印每个<文件>中的匹配行数目
-T, --initial-tab 行首制表符对齐(如有必要)
-Z, --null 在<文件>名最后打印空字符
文件控制:
-B, --before-context=NUM 打印文本及其前面NUM 行
-A, --after-context=NUM 打印文本及其后面NUM 行
-C, --context=NUM 打印NUM 行输出文本
-NUM 等同于 --context=NUM
--color[=WHEN],
--colour[=WHEN] 使用标记高亮匹配字串;
WHEN 可以是“always”、“never”或“auto”
-U, --binary 不要清除行尾的 CR 字符(MSDOS/Windows)
若给定<文件>为“-”,则从读取标准输入。 若无<文件>参数,则除非处于
递归工作模式视为从“.”读取之外,一律视为从“-”读取。如果提供了少于
两个<文件>参数,则默认启用 -h 选项。如果有任意行(或者指定了 -L 选项
并有任意文件)被匹配,则退出状态为 0,否则为 1;如果有错误产生,且未指
定 -q 参数,则退出状态为 2。
测试指令路径,在当前目录下创建了test.txt test1.txt test2.txt 和子目录seconddir,seconddir包含test.txt test1.txt test2.txt ,里面内容都是一样的。
cat test.txt
test1
test2
test3
one
two
three
常用的grep指令
在文件test.txt中搜索test
grep test test.txt
#输出结果 带test的字符全部会输出
test1
test2
test3
在多个文件中进行查找
grep test test.txt test1.txt test2.txt
#输出结果 :墙面是文件名字,后面是搜索到的内容
test.txt:test1
test.txt:test2
test.txt:test3
test1.txt:test1
test1.txt:test2
test1.txt:test3
test2.txt:test1
test2.txt:test2
test2.txt:test3
输出除之外的所有行,-v
grep -v "test" test.txt
#输出结果
one
two
three
标记匹配颜色 –color=auto 选项
grep "test" test.txt --color=auto
grep "test" test.txt --color=always
grep "test" test.txt --color=never
#搜索到的字体是否显示颜色,一般都是红色,默认auto
使用正则表达式 -E
grep -E "[1-9]+"
# 或
egrep "[1-9]+"
使用正则表达式 -P 选项
grep -P "(\d{3}\-){2}\d{4}" file_name
只输出文件中匹配到的部分 -o 选项
echo this is a test line. | grep -o -E "[a-z]+\."
line.
echo this is a test line. | egrep -o "[a-z]+\."
line.
统计文件中包含字符串的行数 -c
grep -c "test" test.txt
#输出结果
3
在文件test.txt中带字符串test的行数一共3行。
history 可以查看使用grep的历史记录
history | grep color
#输出结果
2085 grep "test" test.txt --auto-color
2086 grep "test" test.txt -color=auto
2087 grep "test" test.txt --color=auto
2089 grep "test" test.txt --color=auto
2090 grep "test" test.txt --color=always
2091 grep "test" test.txt --color=never
2092 grep "test" test.txt --color=auto
2096 history | grep color
我之前输入的带color的grep指令都会输出
输出包含匹配字符串的行数
grep -n "test" test.txt
#或
grep "test" -n test.txt
#或
cat test.txt | grep "test" -n
#输出结果
1:test1
2:test2
3:test3
:前显示行数,后面显示内容
打印样式匹配所位于的字符或字节偏移
echo this is my first app | grep -b -o "first"
#输出结果
11:first
#一行中字符串的字符偏移是从该行的第一个字符开始计算,起始值为0。选项 **-b -o** 一般总是配合使用。
搜索多个文件并查找匹配文本在哪些文件中
grep -l "test" test.txt test1.txt
#输出结果
test.txt
test1.txt
grep递归搜索文件
在多级目录下进行递归搜索
grep "test" . -r -n
#或
grep -nr "test"
#输出结果
test2.txt:1:test1
test2.txt:2:test2
test2.txt:3:test3
test1.txt:1:test1
test1.txt:2:test2
test1.txt:3:test3
seconddir/test2.txt:1:test1
seconddir/test2.txt:2:test2
seconddir/test2.txt:3:test3
seconddir/test1.txt:1:test1
seconddir/test1.txt:2:test2
seconddir/test1.txt:3:test3
seconddir/test.txt:1:test1
seconddir/test.txt:2:test2
seconddir/test.txt:3:test3
test.txt:1:test1
test.txt:2:test2
test.txt:3:test3
带不带.都可以执行,.可以省略
忽略匹配的字符大小写 -i
grep -i "TEST" test.txt
#输出结果
test1
test2
test3
选项 -e 制动多个匹配样式
grep -e "test" -e "one" -nr
#输出结果
test2.txt:1:test1
test2.txt:2:test2
test2.txt:3:test3
test2.txt:4:one
test1.txt:1:test1
test1.txt:2:test2
test1.txt:3:test3
test1.txt:4:one
seconddir/test2.txt:1:test1
seconddir/test2.txt:2:test2
seconddir/test2.txt:3:test3
seconddir/test2.txt:4:one
seconddir/test1.txt:1:test1
seconddir/test1.txt:2:test2
seconddir/test1.txt:3:test3
seconddir/test1.txt:4:one
seconddir/test.txt:1:test1
seconddir/test.txt:2:test2
seconddir/test.txt:3:test3
seconddir/test.txt:4:one
test.txt:1:test1
test.txt:2:test2
test.txt:3:test3
test.txt:4:one
grep -e "test" -e "one" -nr -o
#输出结果
test2.txt:1:test
test2.txt:2:test
test2.txt:3:test
test2.txt:4:one
test1.txt:1:test
test1.txt:2:test
test1.txt:3:test
test1.txt:4:one
seconddir/test2.txt:1:test
seconddir/test2.txt:2:test
seconddir/test2.txt:3:test
seconddir/test2.txt:4:one
seconddir/test1.txt:1:test
seconddir/test1.txt:2:test
seconddir/test1.txt:3:test
seconddir/test1.txt:4:one
seconddir/test.txt:1:test
seconddir/test.txt:2:test
seconddir/test.txt:3:test
seconddir/test.txt:4:one
test.txt:1:test
test.txt:2:test
test.txt:3:test
test.txt:4:one
#加上-o只显示非空匹配部分
结合递归查询,搜索test 和 one 字符串
在grep搜索结果中包括或者排除指定文件
#只在目录中test.txt文件中递归搜索test,其他文件不会搜索
grep "test" -nr --include test.txt
# 输出结果
seconddir/test.txt:1:test1
seconddir/test.txt:2:test2
seconddir/test.txt:3:test3
test.txt:1:test1
test.txt:2:test2
test.txt:3:test3
#在搜索结果中排除test.txt
grep "test" -nr --exclude test.txt
#输出结果
test2.txt:1:test1
test2.txt:2:test2
test2.txt:3:test3
test1.txt:1:test1
test1.txt:2:test2
test1.txt:3:test3
seconddir/test2.txt:1:test1
seconddir/test2.txt:2:test2
seconddir/test2.txt:3:test3
seconddir/test1.txt:1:test1
seconddir/test1.txt:2:test2
seconddir/test1.txt:3:test3
#这个暂时没整懂
#排除列表中的文件
grep "test" -nr --exclude-from test.txt
使用0值字节后缀的grep和xargs
# 测试文件:
echo "aaa" > file1
echo "bbb" > file2
echo "aaa" > file3
grep "aaa" file* -lZ | xargs -0 rm
# 执行后会删除file1和file3,grep输出用-Z选项来指定以0值字节作为终结符文件名(\0),xargs -0 读取输入并用0值字节终结符分隔文件名,然后删除匹配文件,-Z通常和-l结合使用。
grep 静默输出 -q
grep -q "test" test.txt
# 不会输出任何信息,如果命令运行成功返回0,失败则返回非0值。一般用于条件测试。
打印出匹配文本之前或者之后的行
#显示匹配某个结果之后的2行,使用 -A 选项
grep test test.txt -A 2
#输出
test1
test2
test3
one
two
#显示匹配某个结果之前的2行,使用 -B 选项
grep two test.txt -B 2
#输出
test3
one
two
#显示匹配某个结果的前2行和后2行,使用 -C 选项
grep one test.txt -C 2
#输出
test2
test3
one
two
three
grep常用指令先到这里,后续有用到其他的继续补充。