Hack 7. Grep Command

本文介绍了grep命令的基本语法及常见选项,如忽略大小写(-i)、倒置匹配(-v)、递归搜索(-r)等,并通过实例展示了如何在文件和目录中高效地查找特定文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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]

[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.txt


Refer:[url]http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/[/url]

--EOF--
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值