在Linux下,你可以使用grep
命令来搜索文本文件中的内容
grep [选项] "要搜索的文本" 文件名
example.txt
的文本文件,你想在其中搜索包含文本hello
的所有行。你可以使用以下命令:grep "hello" example.txt
-i
:忽略大小写-v
:显示不包含指定文本的所有行-r
:递归搜索子目录中的文件-n
:显示匹配行的行号-l
:仅显示包含匹配文本的文件名grep -i "hello" example.txt
hello
的所有行:grep -v "hello" example.txt
.txt
文件:grep -r "hello" --include "*.txt" .
grep -n "hello" example.txt
hello
的文件名:grep -l "hello" example.txt
你可以根据需要组合使用这些选项。要查看所有可用选项,请参阅grep
的手册页:
man grep