在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