grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。
选项
1 | -a 不要忽略二进制数据。 |
grep命令常见用法
在文件中搜索一个单词,命令会返回一个包含“match_pattern”的文本行:1
2grep match_pattern file_name
grep "match_pattern" file_name
例如1
grep import a.py
输出1
2
3
4snjl@VM-0-2-ubuntu:~/item/python/stock$ grep import file_tool.py
import os
import shutil
import time
在多个文件中查找:1
grep "match_pattern" file_1 file_2 file_3 ...
例如:1
2
3
4
5
6
7
8
9
10snjl@VM-0-2-ubuntu:~/item/python/stock$ grep import file_tool.py email_tool.py
file_tool.py:import os
file_tool.py:import shutil
file_tool.py:import time
email_tool.py:from email.header import Header
email_tool.py:from email.mime.text import MIMEText
email_tool.py:from email.mime.multipart import MIMEMultipart
email_tool.py:from email.utils import parseaddr, formataddr
email_tool.py:import random
email_tool.py:import smtplib
输出除之外的所有行 -v 选项:1
grep -v "match_pattern" file_name
同样也可以使用多个文件。
标记匹配颜色 –color=auto 选项:1
grep "match_pattern" file_name --color=auto
其中,color只能为always,never,auto
使用man ls查看:1
2--color[=WHEN]
colorize the output. WHEN defaults to `always' or can be `never' or `auto'.
就是说在什么情况下(总是,从不,或自动)让ls的结果用彩色显示。
例如ubuntu下的ls其实是 ls –color=auto的别名。
使用正则表达式 -E选项(或者是egrep指令)1
2
3grep -E "[1-9]+"
或
egrep "[1-9]+"
只输出文件中匹配到的部分 -o 选项:1
2
3
4
5
6
7echo this is a test line. | grep -o -E "[a-z]+\."
输出: line.
echo this is a test line. | egrep -o "[a-z]+\."
输出:line.
统计文件或者文本中包含匹配字符串的行数 -c 选项:1
grep -c "text" file_name
例如:1
2snjl@VM-0-2-ubuntu:~/item/python/stock$ grep -c import email_tool.py
6
输出包含匹配字符串的行数 -n 选项:
1 | grep "text" -n file_name |
测试:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15snjl@VM-0-2-ubuntu:~/item/python/stock$ grep import -n email_tool.py
1:from email.header import Header
2:from email.mime.text import MIMEText
3:from email.mime.multipart import MIMEMultipart
5:from email.utils import parseaddr, formataddr
6:import random
7:import smtplib
snjl@VM-0-2-ubuntu:~/item/python/stock$ cat email_tool.py | grep import -n
1:from email.header import Header
2:from email.mime.text import MIMEText
3:from email.mime.multipart import MIMEMultipart
5:from email.utils import parseaddr, formataddr
6:import random
7:import smtplib
打印样式匹配所位于的字符或字节偏移:
1 | echo gun is not unix | grep -b -o "not" |
搜索多个文件并查找匹配文本在哪些文件中:
1 | grep -l "text" file1 file2 file3... |
示例:1
2
3
4
5
6
7
8snjl@VM-0-2-ubuntu:~/item/python/stock$ ls
email_tool.py main_test.py
excel_tool.py nohup.out
file_tool.py __pycache__
snjl@VM-0-2-ubuntu:~/item/python/stock$ grep -l import email_tool.py main_test.py
email_tool.py
main_test.py
`
grep递归搜索文件
在多级目录中对文本进行递归搜索:1
2grep "text" . -r -n
# .表示当前目录。
示例:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16snjl@VM-0-2-ubuntu:~/item/python/stock$ grep import . -r -n
./email_tool.py:1:from email.header import Header
./email_tool.py:2:from email.mime.text import MIMEText
./email_tool.py:3:from email.mime.multipart import MIMEMultipart
./email_tool.py:5:from email.utils import parseaddr, formataddr
./email_tool.py:6:import random
./email_tool.py:7:import smtplib
./file_tool.py:1:import os
./file_tool.py:2:import shutil
./file_tool.py:3:import time
./excel_tool.py:1:from openpyxl import Workbook
./excel_tool.py:2:import csv
./main_test.py:1:import time
./main_test.py:3:import email_tool
./main_test.py:4:import file_tool
./main_test.py:5:import excel_tool
忽略匹配样式中的字符大小写:
1 | echo "hello world" | grep -i "HELLO" |
选项 -e 制动多个匹配样式:
1 | echo this is a text line | grep -e "is" -e "line" -o |
例如:1
2
3
4
5
6
7
8
9
10
11
12snjl@VM-0-2-ubuntu:~/item/python/stock$ grep import -o -e "import" -e "email" main_test.py
grep: import: No such file or directory
main_test.py:import
main_test.py:import
main_test.py:email
main_test.py:import
main_test.py:import
main_test.py:email
main_test.py:email
main_test.py:email
main_test.py:email
main_test.py:email
1 | snjl@VM-0-2-ubuntu:~/item/python/stock$ clear |
在grep搜索结果中包括或者排除指定文件:1
2
3
4
5
6
7
8#只在目录中所有的.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:1
2
3
4
5
6
7
8#测试文件:
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静默输出:1
2
3grep -q "test" filename
#不会输出任何信息,如果命令运行成功返回0,失败则返回非0值。一般用于条件测试。
打印出匹配文本之前或者之后的行:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#显示匹配某个结果之后的3行,使用 -A 选项:
seq 10 | grep "5" -A 3
5
6
7
8
#显示匹配某个结果之前的3行,使用 -B 选项:
seq 10 | grep "5" -B 3
2
3
4
5
#显示匹配某个结果的前三行和后三行,使用 -C 选项:
seq 10 | grep "5" -C 3
2
3
4
5
6
7
8
#如果匹配结果有多个,会用“--”作为各匹配结果之间的分隔符:
echo -e "a\nb\nc\na\nb\nc" | grep a -A 1
a
b
--
a
b