grep命令的一些高级用法

一般用 grep只作简单的匹配,或者用于管道输出信息。例如

# apt-cache search vim|grep enhanced

有时候也需要更复杂的操作,例如

显示行号

# wget //qiniu.uqugu.com/doc/txt/the-end-of-the-world.txt
# grep -n 'goodbye' the-end-of-the-world.txt

输出

show-number

忽略大小写

# grep -n -i 'Goo.' the-end-of-the-world.txt

输出

ignore-case

反向选择

# grep -v 'pattern'

打出匹配行后几行

# grep -A 2 'goodbye' the-end-of-the-world.txt

输出

show-after-lines

打出匹配行前3行

# grep -B 3 'goodbye' the-end-of-the-world.txt

输出

show-before-lines

循环查找

这也是经常用的一种模式,例如查找本地的官方HTML文档快速获取帮助,快速定位开源软件的函数等等。

# mkdir lyric
# wget -O lyric/love-paradise.txt '//qiniu.uqugu.com/doc/txt/love-paradise.txt'
# grep -r love

输出

grep-recursive

哇哦,就是一个这么常用的grep命令也是相当强悍的,看怎么用了。

关于本文如您有任何想法和意见,欢迎与我们联系,邮箱地址zhi@uqugu.com
您对本文有什么看法,喜欢或者不喜欢都可以发表意见。