我在這里有這些命令用于查看檔案以查看它們是否包含某個正則運算式:
find / -type f | xargs grep -ilIs <regex>
它似乎做了它應該做的事情(查看我桌面上的每個檔案以獲取運算式),但理想情況下我不會顯示此錯誤訊息,因為它只評論上述命令找到的檔案中不匹配的單引號:
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
我嘗試使用 sed 來消除錯誤訊息,但是| sed '/xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option/d'在命令之后使用并沒有像我認為的那樣將其洗掉。我想知道你們中是否有人知道任何可以消除 xargs 錯誤訊息的工具(當然,最好是易于閱讀且打字量最少)。包含-0作為引數不會回傳除此之外的任何內容:
xargs: argument line too long
uj5u.com熱心網友回復:
xargs有它自己的轉義語法,例如:
$ echo 'file 1.txt' | xargs printf '<%s>\n'
<file>
<1.txt>
$ echo '"file 1.txt"' | xargs printf '<%s>\n'
<file 1.txt>
所以你不能給它提供原始檔案路徑,因為它們可以包含除NUL位元組之外的任何字符。
為了解決這個問題,大多數實作xargs都有-0允許處理NUL-delimited 記錄的開關,但您需要NUL在輸入流中提供位元組:
$ printf '%s\n' 'file 1.txt' 'file 2.txt' | xargs -0 printf '<%s>\n'
<file 1.txt
file 2.txt
>
$ printf '%s\0' 'file 1.txt' 'file 2.txt' | xargs -0 printf '<%s>\n'
<file 1.txt>
<file 2.txt>
最后,您可以通過三種方式正確完成任務:
find ... -print0 | xargs -0 grep ...
find / -type f -print0 | xargs -0 grep -ilIs 'regex'
find ... -exec grep ... {}
find / -type f -exec grep -ilIs 'regex' {}
grep -R ...
grep -iRlIs 'regex' /
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/532824.html
標籤:重击awksed
下一篇:案例陳述句不匯出變數
