每天一个Linux命令 | 您所在的位置:网站首页 › linux里grep › 每天一个Linux命令 |
grep、sed、awk被称为文本处理三剑客。对于纯文本来说,没有这三个工具干不了的事情,并且效率也不低。今天给大家介绍一下其中的grep。 我觉得grep非常好用。对文本的搜索功能非常强大。 在文件中搜索一个单词,命令会返回一个包含“match_pattern”的文本行: grep match_pattern file_name grep "match_pattern" file_name在多个文件中查找: grep "match_pattern" file_1 file_2 file_3 ...输出除之外的所有行 -v 选项: grep -v "match_pattern" file_name标记匹配颜色 --color=auto 选项: grep "match_pattern" file_name --color=auto使用正则表达式 -E 选项: grep -E "[1-9]+" 或 egrep "[1-9]+"只输出文件中匹配到的部分 -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 "text" file_name输出包含匹配字符串的行数 -n 选项: grep "text" -n file_name 或 cat file_name | grep "text" -n #多个文件 grep "text" -n file_1 file_2打印样式匹配所位于的字符或字节偏移: echo gun is not unix | grep -b -o "not" 7:not #一行中字符串的字符便是从该行的第一个字符开始计算,起始值为0。选项 -b -o 一般总是配合使用。搜索多个文件并查找匹配文本在哪些文件中: grep -l "text" file1 file2 file3...grep递归搜索文件 在多级目录中对文本进行递归搜索: grep "text" . -r -n # .表示当前目录。忽略匹配样式中的字符大小写: echo "hello world" | grep -i "HELLO" hello选项 -e 指的多个匹配样式: echo this is a text line | grep -e "is" -e "line" -o is line #也可以使用-f选项来匹配多个样式,在样式文件中逐行写出需要匹配的字符。 cat patfile aaa bbb echo aaa bbb ccc ddd eee | grep -f patfile -o在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: #测试文件: echo "aaa" > file1 echo "bbb" > file2 echo "aaa" > file3 grep "aaa" file* -lZ | xargs -0 rm #执行后会删除file1和file3,grep输出用-Z选项来指定以0值字节作为终结符文件名(),xargs -0 读取输入并用0值字节终结符分隔文件名,然后删除匹配文件,-Z通常和-l结合使用。grep静默输出: grep -q "test" filename #不会输出任何信息,如果命令运行成功返回0,失败则返回非0值。一般用于条件测试。打印出匹配文本之前或者之后的行: #显示匹配某个结果之后的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 b c a b c" | grep a -A 1 a b -- a b |
CopyRight 2018-2019 实验室设备网 版权所有 |