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