第十六章 在文件中搜索文本工具:grep命令 和egrep命令

第十六章 在文件中搜索文本工具:grep命令 和egrep命令

名词解释

grep(global search regular expression(RE)and print out the line,全面搜索正则表达式并把行打印出来)
grep是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

选项

- -a :不要忽略二进制数据
- -A <显示行数>:除了显示符合范本样式的那一行之外,并显示该行之后的内容。
- -b :在显示符合范本样式的那一行之外,并显示该行所有的内容。
- -B <显示行数>:除了显示符合范本样式的那一行之外,并显示该行之前的内容。
- -c ,--count : 计算符合范本样式的行数。
- -C <显示列数>或-<显示列数>:除了显示符合范本样式的那一行之外,并显示 该行 的前后行内容。
- -d <进行动作>:当指定要查找的是目录而非文件时,必须使用这项参数,否则grep命令将汇报信息并停止动作。-d recurse 递归查找的意思。
- -e <范本样式>:指定字符串作为查找文件内容的范本样式。
- -E :将范本样式为延伸的普通表示法来使用,意味着能使用扩展正则表达式。
- -f <范本文件>:指定范本文件,其内容有一个或多个 范本样式,让grep查找符合范本条件的文件内容,格式为每一行的范本样式。
- -F :将范本样式视为固定字符串的列表。
- -G :将范本样式视为普通的表示法来使用。
- -h :在显示符合范本样式的那一列之前,不表示该列所属的文件名称。
- -H :在显示符合范本样式的那一列之前,表示该列的文件名称。
- -i :忽略字符大小写的差别。
- -l :列出文件内容符合指定的范本样式的文件名称。
- -L :列出文件内容不合符指定的范本样式的文件名称。
- -n :在显示符合范本昂是的那一行之前,并打印行号。
- -q :不显示任何信息。
- -R / -r :此参数的效果和指定"-d recurse" 一样,递归查找的意思。
- -s :不显示错误信息。
- -v :反转查找。
- -w :只显示全字符合的列(全字匹配)。
- -x :只显示全列符合的列。
- -y :此参数效果跟"-i" 相同。
- -o :只输出文件中匹配到的部分。
-  -Z : --null   print 0 byte after FILE name

grep 命令常见用法

在文件中搜索一个单词,命令会返回一个包含"match_pattern"的文本行:

grep match_pattern file_name
grep "match_pattern" file_name

在多个文件中查找:

grep "match_pattern" file_1 file_2 file_3 ...

输出除了‘match_pattern’之外的所有行 -v选项:

grep -v "match_pattern" file_name

标记匹配颜色 --color=auto 选项:

grep "match_pattern" file_name --color=auto

使用正则表达式-E选项:

grep -E "[1-9]+" test.txt  #匹配文件里的所有数字
或者
egrep "[1-9]+" test.txt

例子:
[root@ceshi grep]# echo "this 1 is 2 a 3 test 4 line." | grep -o -E "[1-9]+"  
1
2
3
4

只输出文件中匹配到的部分-o 选项:

[root@ceshi grep]# echo "this is a test line." | grep -o -E "[a-z]+\."
line.

或者
[root@ceshi grep]# echo "this is a test line." | egrep -o "[a-z]+\."
line.

统计文件或者文本中包含匹配字符串的行数-c 选项:

[root@ceshi grep]# grep -c "a" test.txt 
2

输出包含匹配字符串的行数,并打印行号 -n 选项:

[root@ceshi grep]# grep -n "a" test.txt  
1:aaaaaaaaaa
2:aaaaaaaaaa

或者
[root@ceshi grep]# cat test.txt | grep "a" -n
1:aaaaaaaaaa
2:aaaaaaaaaa

多个文件查找:
[root@ceshi grep]# grep -n "a" test.txt test2.txt 
test.txt:1:aaaaaaaaaa
test.txt:2:aaaaaaaaaa
test2.txt:1:aaaaaaaaaa
test2.txt:2:aaaaaaaaaa

打印匹配到的字符所在的字符位置(字节偏移)和字符:

[root@ceshi grep]# echo "this is a test line." | grep -b -o "test"
10:test

#一行中字符串所在的位置从该行的第一个字符开始计算,起始值为0;选项-b -o 一般总是配合使用。

搜索多个文件并查找匹配文本在那些文件中:

#搜索文件内容包含a 的文件名都打印出来
[root@ceshi grep]# grep -l "a" test.txt test2.txt   
test.txt
test2.txt

grep递归搜索文件

在多级目录中对文本进行递归搜索:

[root@ceshi grep]# grep "a" . -r -n
./test.txt:1:aaaaaaaaaa
./test.txt:2:aaaaaaaaaa
./test2.txt:1:aaaaaaaaaa
./test2.txt:2:aaaaaaaaaa

或者
[root@ceshi grep]# grep "a" . -d recurse -n
./test.txt:1:aaaaaaaaaa
./test.txt:2:aaaaaaaaaa
./test2.txt:1:aaaaaaaaaa
./test2.txt:2:aaaaaaaaaa

# .代表当前目录
# -r 、-R、-d recurse 都是递归查找的意思

忽略匹配样式中的字符大小写:

[root@ceshi grep]# echo "Hello WorlD" | grep -i "hello" 
Hello

选项-e指定多个匹配样式:

例1:
[root@ceshi grep]# echo "this is a text line" | grep -e "is" -e "line" -o
is
is
line

[root@ceshi grep]# echo this is a text line | grep -e "is" -e "line" -o -w
is
line

# -w:全自符匹配,也就是完全匹配字符串,而不是模糊匹配

例2:
#也可以使用-f选项来匹配多个样式,在样式文件中逐行写出需要匹配的字符。
[root@ceshi grep]# cat test3.txt 
aaa
aaaa
111
bbb
22222222222
bbbb
6666666666
ccc
cccc
#匹配出test3.txt 文件中包含 aaa bbb ccc ddd的字符
[root@ceshi grep]# echo "aaa bbb ccc ddd
" | grep -f test3.txt -o
aaa
bbb
ccc
#由于test3.txt中没有ddd,所有没有匹配到,也没有被打印。

在grep搜索结果中包括 或者 排除指定文件:

#在目录中查找所以.php和.html文件中递归搜索字符"main()"
grep "main()" . -r --include *.{php,html}

#在搜索结果中排除所有README文件
grep "main()" . -r --exclude "README"

#在搜索结果中排除filelist文件列表里的文件
grep "main()" . -r --exclude-from filelist

使用0值字节后缀的grep与xargs

[root@ceshi grep]# echo "aaa" > file1
[root@ceshi grep]# echo "bbb" > file2    
[root@ceshi grep]# echo "ccc" > file3 

#在所有file开头文件中查找包含“aaa”的文件,然后删除匹配文件
[root@ceshi grep]# grep "aaa" file* -lZ | xargs -0 rm 

[root@ceshi grep]# ls
a.php  b.html  file2  file3  test2.txt  test3.txt  test.txt

#执行后file1倍删除掉了。
#grep 输出用-Z 选项来指定以0值字节作为终结符文件名(\0)
#xargs -0来读取输入并用0值字节终结符 分隔文件名,然后删除匹配文件,-Z和-l 通常联合使用。

grep静默输出:

[root@ceshi grep]# grep -q "test" file2
[root@ceshi grep]# echo $?
1

#-q 不显示任何信息;如果命令运行成功返回0,失败则非0,一般用于条件测试。
#用echo $? 测试 条件成功与否。

打印出匹配文本之前或者之后的行:

#显示匹配某个结果之后的3行,使用-A 选项:
[root@ceshi grep]# seq 10 | grep "5" -A 3
5
6
7
8

#显示匹配某个结果之前的3行,使用-B 选项:
[root@ceshi grep]# seq 10 | grep "5" -B 3
2
3
4
5

#显示匹配某个结果前3行和后3行,使用-C 选项:
[root@ceshi grep]# seq 10 | grep "5" -C 3
2
3
4
5
6
7
8

egrep命令

egrep也是在文件内查找指定的字符串。egrep类似grep -E 使用效果,使用语法及参数 参考grep指令,与grep不同点在于 解读字符串的方法。

(grep -E :将范本样式为延伸的普通表示法来使用,意味着能使用扩展正则表达式。)

egrep解读方法:extended regular expression

grep的解读方法:basic regular expression

前者比后者表达更规范。

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...