從命令列使用egrep 和正規表示式不起作用,我在標題中收到錯誤。
egrep -rnw /var/log/usres/([a-z]+).([a-z]+)\/([a-z]+).conf/user.settings -e "pattern"
我已經驗證了正規表示式https://regex101.com/r/pL5pW4/1並使用 shellcheck.net 都說沒有問題。我想做的是匹配特定資料夾,如下所示:
egrep -rnw /var/log/users/sd.dfr/test.conf/user.settings, so the
本例中的「sd.dfr」和「test」就是正規表示式的用途。將其中的任何組合與資料夾路徑相符。我嘗試過引用、轉義、雙引號。為什麼這不起作用?
答案1
grep
搜尋正規表示式在內容中文件數量。它不搜尋名稱匹配的文件常用表達。 (用於find
此目的。)此外,在 grep 看到它們之前,您提供的檔案/目錄名稱清單將由 shell 解釋。 shell 使用以下命令匹配文件 球體不是正規表示式。
Shell 通配符不如正規表示式精確,但以下內容可能足夠適合您:
egrep -rnw /var/log/users/*.*/*.conf/user.settings -e "pattern"
或者,更好的是,因為egrep
已棄用:
grep -Ernw /var/log/users/*.*/*.conf/user.settings -e "pattern"