Hack 7. Grep Command

grep 命令在我们的日常操作中应用的很普遍。很多时候我们都会用到grep命令从日志或者从文件中来检索信息。
[语法]
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--

猜你喜欢

转载自xfxlch.iteye.com/blog/2212990