如何使用正則運算式檢查檔案是否包含同一行“這是 123”(帶有縮進)精確三遍?
檔案:
<dev>
<source file='/path/to/file'/>
<string>this is 123</string>
</dev>
<dev>
<source dev='this dev'/>
<string>this is 123</string>
</dev>
<dev>
<source dev='another dev'/>
<string>this is 123</string>
</dev>
uj5u.com熱心網友回復:
您可以使用 awk 來計算行數
awk '/ <string>this is 123</string>/ { count } END { print count }' file.txt
要使用的字串包含在/s 中。在 awk 遇到的每一行之后,它都會增加 acount并且當它到達 END 時,它會列印計數
使用該計數在條件中進行比較
c=$(awk '/ <string>this is 123</string>/ { count } END { print count }' file.txt)
if [[ c -eq 3 ]]; then
# do stuff
fi
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/407365.html
標籤:
