![](/img/trans.png)
[英]Trying to have grep tell me which values are not present, from a foreach loop
[英]How to get ripgrep to tell me which expressions from a list have no matches on the filesystem
例如,假設我有要搜索的字符串列表:
alfa bravo charlie delta nebuchadnezzar bartholomew
在我的倉庫中有包含alfa
、 bravo
、 charlie
和delta
的文件,但沒有包含nebuchadnezzar
的文件,也沒有包含bartholomew
的文件。 然后我希望答案是:
nebuchadnezzar bartholomew
正如您可能猜到的那樣,我正在尋找不推薦使用的東西。 我最終使用了以下 Ruby 代碼解決方法,因為在嘗試man rg
后我無法找到解決方案。
%w[alfa bravo charlie delta nebuchadnezzar bartholomew].each do |word|
command = 'rg ' + word
if `#{command}` == '' # execute the command, see if ripgrep found nothing
puts word
end
end
當在簡單的 shell 循環構造中找不到匹配項時,您可以使用rg
的退出代碼。 從docs看來,當沒有找到正則表達式的匹配項並且沒有看到錯誤時,它似乎返回了一個代碼 1。 采用它
for word in alfa bravo charlie delta nebuchadnezzar bartholomew; do
rg "$word" >/dev/null 2>&1
[ "$?" -eq 1 ] && printf '%s\n' "no match for $word"
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.