Eddy Hernandez
Eddy Hernandez

Reputation: 5856

Extract part of the string elements in an array using jq

I'm trying to extract part of the string in elements of an array, and create a new array with these extractions.

[
  "local/binaries/app-2.21.0.tar.gz",
  "local/binaries/app-2.20.0.tar.gz",
  "local/binaries/app-2.19.1.tar.gz",
  "local/binaries/app-2.19.0.tar.gz",
  "local/binaries/app-2.18.0.tar.gz"
]

Desired output

[
  "app-2.21.0",
  "app-2.20.0",
  "app-2.19.1",
  "app-2.19.0",
  "app-2.18.0"
]

Upvotes: 1

Views: 1044

Answers (1)

geobreze
geobreze

Reputation: 2422

You can use jq's capture function with regular expressions.

jq '[.[] | capture("(?<captured>app-[0-9]+\\.[0-9]+\\.[0-9]+)") | .[]]'

Try it out on jq playground.

Documentation: https://stedolan.github.io/jq/manual/#RegularexpressionsPCRE

Upvotes: 4

Related Questions