[语法]
grep [options] pattern [files]
[选项]
-i (ignore case) -v (invert-match) -r(recursive 递归) -c(count) 太多了,自己执行man grep来查看
[实例]
[[email protected] helloworld]$ cat hellworld.txt --it is a sample for grep demo-- by Jack! __EOF__ [[email protected] helloworld]$ grep Jack hellworld.txt by Jack! [[email protected] helloworld]$ grep -i jack hellworld.txt by Jack! [[email protected] helloworld]$ grep -v Jack hellworld.txt --it is a sample for grep demo-- __EOF__ [[email protected] helloworld]$ grep -c Jack hellworld.txt 1 [[email protected] helloworld]$ grep -vi jack hellworld.txt --it is a sample for grep demo-- __EOF__ [[email protected] helloworld]$
如果你要查找当前目录及其子目录的有哪些包含查找字段,可以用-r参数,显示格式是:
文件名:匹配关键字的上下文
[[email protected] hello]$ grep -r Jack /home/staff/clu/helloworld/ /home/staff/clu/helloworld/hello/hello.txt:By Jack /home/staff/clu/helloworld/hellworld.txt:by Jack!
如果你不想把后面的上下文,可以用-l命令来格式化。
[[email protected] hello]$ grep -rl Jack /home/staff/clu/helloworld/ /home/staff/clu/helloworld/hello/hello.txt /home/staff/clu/helloworld/hellworld.txt
Refer: http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
--EOF--