1、grep(全称:Global Regular Expression Print)命令用于根据给定的正则表达式搜索文本,并将匹配的行打印出来。注意匹配时最好把引号加上
2、在一个或者多个文件中搜索一个关键词,并将关键词用颜色标记出来:
grep ”yanghongqi“ filename1 filename2 filename3 --colour
3、也可以输出除了被查找行之外文件的剩余行:
grep -v “yanghongqi” filename1 filename2 filename3
4、也可以查找perl中的正则表达式 ,用-P选项,如下表示查找数字1到9的一个或者多个字符,并用颜色标记出来:
grep -E “[1-9]+” filename1 filename2 filename3
perl中的正则表达式可以参考https://blog.51cto.com/u_16131207/6366871
5、只输出匹配到的部分字符:
echo this is a test line. | grep -o -E "[a-z]+\."
输出为line.
6、匹配到的字符串在文件中占多少行:
grep "yanghongqi" -n filename
7、命令行搜索输入某个命令的次数:
history | grep git
8、搜索当前文本在哪个文件中
grep -l "yanghongqi" filename1 filename2 filename3
9、忽略匹配的大小写
echo "hello world" | grep -o -i "HELLO"
10、可以连续用两个-e选项匹配多个字符串:
echo "this is a apple" | grep -o -e "is" -e "apple"