grep文本处理工具,搜索文本,把符合条件的行打印出来
匹配某个字符:
grep ‘test’ test.txt
匹配空字符:
grep ‘^$’ test.txt
匹配以某个字符开头的行:
grep '^test' test.txt
匹配以某个字符结尾的行:
grep 'test$' test.txt
反向选择:
grep -v '#' test.txt 取出所有未被注释的行
打印行号:
grep -n 'test' test.txt
把匹配出来的字符高亮显示:
grep 'test' test.txt --color=auto
--color=(never,always,auto)三个选项
忽略大小写匹配:
grep -i 'test' test.txt
显示匹配行以及匹配行前几行:
grep -B 3 'test' test.txt
-B before
显示匹配行以及匹配行后几行:
grep -A 3 'test' test.txt
-A after
匹配任意字符任意次:
grep "." test.txt
\< \>分别标注单词开头,开头结尾:
grep '\<i\>' aaa.txt
grep -o 只输出文件中匹配到的部分
去空白行和注释:
egrep -v "(#|^$)" filename(文件名)
转自:https://blog.51cto.com/13917261/2158919