grep 命令在我们的日常操作中应用的很普遍。很多时候我们都会用到grep命令从日志或者从文件中来检索信息。
[b][语法][/b]
[u]grep [options] pattern [files][/u]
[b][选项][/b]
-i (ignore case) -v (invert-match) -r(recursive 递归) -c(count) 太多了,自己执行man grep来查看
[b][实例][/b]
如果你要查找当前目录及其子目录的有哪些包含查找字段,可以用-r参数,显示格式是:
文件名:匹配关键字的上下文
如果你不想把后面的上下文,可以用-l命令来格式化。
Refer:[url]http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/[/url]
--EOF--
[b][语法][/b]
[u]grep [options] pattern [files][/u]
[b][选项][/b]
-i (ignore case) -v (invert-match) -r(recursive 递归) -c(count) 太多了,自己执行man grep来查看
[b][实例][/b]
[clu@portal.ny1 helloworld]$ cat hellworld.txt
--it is a sample for grep demo--
by Jack!
__EOF__
[clu@portal.ny1 helloworld]$ grep Jack hellworld.txt
by Jack!
[clu@portal.ny1 helloworld]$ grep -i jack hellworld.txt
by Jack!
[clu@portal.ny1 helloworld]$ grep -v Jack hellworld.txt
--it is a sample for grep demo--
__EOF__
[clu@portal.ny1 helloworld]$ grep -c Jack hellworld.txt
1
[clu@portal.ny1 helloworld]$ grep -vi jack hellworld.txt
--it is a sample for grep demo--
__EOF__
[clu@portal.ny1 helloworld]$如果你要查找当前目录及其子目录的有哪些包含查找字段,可以用-r参数,显示格式是:
文件名:匹配关键字的上下文
[clu@portal.ny1 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命令来格式化。
[clu@portal.ny1 hello]$ grep -rl Jack /home/staff/clu/helloworld/
/home/staff/clu/helloworld/hello/hello.txt
/home/staff/clu/helloworld/hellworld.txtRefer:[url]http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/[/url]
--EOF--
本文介绍了grep命令的基本语法及常见选项,如忽略大小写(-i)、倒置匹配(-v)、递归搜索(-r)等,并通过实例展示了如何在文件和目录中高效地查找特定文本。
953

被折叠的 条评论
为什么被折叠?



