一般用 grep只作简单的匹配,或者用于管道输出信息。例如
# apt-cache search vim|grep enhanced
有时候也需要更复杂的操作,例如
显示行号
# wget //qiniu.imesu.co/doc/txt/the-end-of-the-world.txt
# grep -n 'goodbye' the-end-of-the-world.txt
输出
忽略大小写
# grep -n -i 'Goo.' the-end-of-the-world.txt
输出
反向选择
# grep -v 'pattern'
打出匹配行后几行
# grep -A 2 'goodbye' the-end-of-the-world.txt
输出
打出匹配行前3行
# grep -B 3 'goodbye' the-end-of-the-world.txt
输出
循环查找
这也是经常用的一种模式,例如查找本地的官方HTML文档快速获取帮助,快速定位开源软件的函数等等。
# mkdir lyric
# wget -O lyric/love-paradise.txt '//qiniu.imesu.co/doc/txt/love-paradise.txt'
# grep -r love
输出
哇哦,就是一个这么常用的grep命令也是相当强悍的,看怎么用了。