Json

jq - 選擇以字元串開頭的屬性

  • March 12, 2021

輸入json:

[
 {
   "id": "89",
   "hostname": "abcd"
 },
 {
   "id": "89",
   "hostname": "babcd"
 }
]

如何修改以下過濾器以僅輸出以“abcd”開頭的主機名?

$ jq -r '.[]|select(.hostname | contains("abcd"))' jjjj
{
 "id": "89",
 "hostname": "abcd"
}
{
 "id": "89",
 "hostname": "babcd"
}
$ jq -r '.[]|select(.hostname | contains("^abcd"))' jjjj
$

解決方案:

jq -r '.[]|select(.hostname | startswith("abcd"))' jjjj

引用自:https://unix.stackexchange.com/questions/443290