具有不同正規表示式的 grep -c 到 bc 的管道差異

具有不同正規表示式的 grep -c 到 bc 的管道差異

我需要查找文件中不包含某些字母集的單字數(每行 1 個單字)。為此,我想取grep -c '.' <file>和的差異grep -c '<other_pattern>' <file>。我嘗試使用以下命令透過管道傳輸到 bc echo "(grep -c '.' <file>) - (grep -c '<other_pattern>' <file>)" | bc。結果只是說文法錯誤。我希望有人能夠了解 bc 接受 grep 呼叫結果所需的語法。我也需要在一個命令中完成此操作。

提前致謝。

答案1

您只是缺少兩個美元符號來啟用命令替換:

echo $(grep -c '.' <file>) - $(grep -c '<other_pattern>' <file>) | bc

我還刪除了引號,因為它們並不是真正必要的。

相關內容