Home > database >  jq filter to strip out the double colons from a string in a json
jq filter to strip out the double colons from a string in a json

Time:07-27

I have a use case where I need to strip out the double colons to get the list of IP addresses.

Input: (test.json)

{
  "source_name": "ABC::10.0.0.1::XYZ ABC::10.0.0.2::XYZ ABC::10.0.0.3::XYZ"
}

Desired output:

10.0.0.1
10.0.0.2
10.0.0.3

Could anyone help with this? Any inputs appreciated

CodePudding user response:

Split the string on spaces, then use substitution to remove the string before and after double colons:

jq -r '.source_name | split(" ")[] | sub("^.*?::"; "") | sub("::.*"; "")' file.json

CodePudding user response:

Try this :

jq -r '.source_name | match("::(.*?)::"; "g") | .captures[].string'

CodePudding user response:

Here's another approach using scan:

jq -r '.source_name | scan("::(.*?)::")[]'
10.0.0.1
10.0.0.2
10.0.0.3

Demo

  • Related