Linux命令 -- grep

查找文件内容,输出满足条件的行

文件完整内容如下

[hdfs@vml ~]$ cat asd.txt
hello
hello world
Hello
Hello World in the asd.txt
today is a sunny day
# 查找文件内容包含 hello 的行 
[hdfs@vml ~]$ grep hello asd.txt
hello
hello world

# 查找内容包含 hello 的行,并且显示行数
[hdfs@vml ~]$ grep -n hello asd.txt
1:hello
2:hello world

# 反向查找,查找文件内容不包含 hello 的行 
[hdfs@vml ~]$ grep -v hello asd.txt
Hello
Hello World in the asd.txt
today is a sunny day

# 不区分大小写
[hdfs@vml ~]$ grep -i hello asd.txt
hello
hello world
Hello
Hello World in the asd.txt

# 精确匹配,查找内容为 hello 的行
[hdfs@vml ~]$ grep -w hello asd.txt
hello

# 多条件查找
[hdfs@vml ~]$ grep -e hello -e today asd.txt
hello
hello world
today is a sunny day

# 用-E参数,启用正则表达式
[hdfs@vml ~]$ grep -E 'hello|today' asd.txt
hello
hello world
today is a sunny day

递归查找目录下的子文件

# 查找testdir目录下的子文件,内容包含hello的行
[hdfs@vml do]$ grep -r hello testdir/
testdir/test3.txt:hello world
testdir/test3.txt:hello today
testdir/testdir1/test11.txt:this is hello 

# 只显示文件名
[hdfs@vml do]$ grep -lr hello testdir/
testdir/test3.txt
testdir/testdir1/test11.txt
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值