我正在撰寫一個內部看起來像這樣的 awk 腳本:
awk -v searchValue="$searchValue" '{ if ($3 == searchValue){
printf NR".", $1, $2, $3, $4
}
else if ($3!=searchValue) {print "The value that you entered is not available. Please try again."}
}' file
這是我的檔案。
One Two 20 100
Three Four 10 500
Five Six 30 800
Seven Eight 20 500
這會嘗試列印一次 else if 方法,但它會列印所有不包含搜索值的行。如果我輸入 20,我的輸出如下所示:
1. One Two 20 100
The value that you entered is not available. Please try again.
The value that you entered is not available. Please try again.
4. Four Five 20 500
如果輸出一次,我必須使用 awk 命令列印 else 。假設我輸入了 700。我該如何解決它才能得到這個:
The value that you entered is not available. Please try again.
uj5u.com熱心網友回復:
也許使用該END子句的解決方案?如果找到匹配項,您可以設定一個變數,然后僅在未找到匹配項時使用ifinEND列印訊息。
awk -v searchValue="$searchValue" '
$3 == searchValue { ismatch = 1; print NR".", $0; }
END {
if(!ismatch) print "The value that you entered is not available. Please try again."
}' file
另外,我不確定您是否真的想使用printf...普通的舊print版本似乎更適合這里。而且$0比上市要容易一些$1, $2, $3, $4。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/413769.html
標籤:
上一篇:讀取用戶空間中ebpf的BPF_MAP_TYPE_HASH的更新值|可以使用read()linux函式獲取通過map共享的物件的當前值嗎
下一篇:有沒有一種方法可以在ebpf程式和用戶空間程式之間共享一些ebpf映射,該映射具有使用libbpf作為鍵的值結構
