32

input json:

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

How to modify below filter to get on output only hostname beginning with "abcd"?

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

Solution:

jq -r '.[]|select(.hostname | startswith("abcd"))' jjjj
1
  • 1
    For anyone wondering, you can also use endswith() if that's what you want to check – lxop Jul 29 '20 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.