Home > Net >  Using JQ and regex to extract just matching regex string
Using JQ and regex to extract just matching regex string

Time:05-02

I have the following input json which I obtain from a curl command and I'm feeding it to jq.

{
  "version": "14.10.0-ee",
  "revision": "ad109bc62af"
}

I'm trying to use jq to extract just '14.10.0'.

I have the following jq command but it's just returning "14.10.0-ee"

jq '. | select(.version|capture("^[0-9]{1,}.[0-9]{1,}.[0-9]{1,}")).version'

I've looked at the jq documentation here and I'm not able to figure the correct syntax. I've tried scan, capture, and match without success.

I am able to achieve what I want if I pipe the result to grep but I would prefer to do it all in one command.

Any help would be greatly appreciated.

CodePudding user response:

.version|scan("^[^-] ")

or

.version|scan("^[0-9.] ")

Don't overcomplicate it ;)

CodePudding user response:

You can split on the hyphen and then take the first element:

.version|split("-")|first

CodePudding user response:

Yet you can use capture through [[:digit:].] pattern

jq -r '. | (.version| capture("(?<v>[[:digit:].] )").v)'

Demo

  • Related