简体   繁体   中英

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"
]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM