grep 是一個強大的文本搜索工具,它允許您在文件中搜索特定的文本模式
grep [options] pattern [file]
其中,options 是可選的命令行參數,pattern 是您要搜索的文本模式,file 是要搜索的文件。
-i:忽略大小寫-v:反向匹配,顯示不包含指定模式的行-r 或 -R:遞歸搜索子目錄中的文件-n:顯示匹配行的行號-l:僅顯示包含匹配文本的文件名-c:顯示匹配行的數量-o:僅顯示匹配的部分,而不是整行example.log 中搜索包含 “error” 的行:grep "error" example.log
example.log 中搜索包含 “Error” 或 “ERROR” 的行(忽略大小寫):grep -i "error" example.log
.log 文件中搜索包含 “error” 的行:grep -r "error" *.log
example.log 中搜索不包含 “success” 的行:grep -v "success" example.log
example.log 中搜索包含 “error” 的行,并顯示行號:grep -n "error" example.log
.log 文件中搜索包含 “error” 的行,并顯示包含匹配文本的文件名:grep -rl "error" *.log
example.log 中搜索包含 “error” 的行,并顯示匹配行的數量:grep -c "error" example.log
example.log 中僅顯示匹配 “error” 的部分:grep -o "error" example.log
這些示例僅涉及 grep 的基本功能。grep 還有許多其他選項和用法,您可以通過閱讀手冊頁(man grep)或在線搜索更多信息來了解更多。