Score:0

Extract keys from yaml file using awk

ru flag

Let's say I have a yaml file like this one:

foo:
  bar: 1

env:
  firstVar: true
  secondVar: 20
  aa_thirdVar: 'hello'
  aa_fourthVar: false

and I need to get the keys under the env key except for the keys with aa_ prefix as a bash array in order to use the array in a bash script.

Score:1
cn flag

Using the yq command from the https://kislyuk.github.io/yq/ a proper tool to parse the YAML format files, you could do:

yq -r '.env|to_entries[]|
    if (.key|test("^[^a][^a][^_]*$")) then
        ( .key + "=" + (.value | @sh) )
    else
        empty
    end
' infile

which outputs:

firstVar=true
secondVar=20

now you only need to export these sets of variables

export $(yq ... )

or create an array:

IFS=$'\n' arr=( $(yq ... ) )

In the ( .key + "=" + (.value | @sh) ) part, it concatenates the pairs of the key-value that those are produced by using the to_entries function and the @sh syntax (which is known as the "Format String and Escaping") is used to provide the POSIX sh shell escape formatting on values.

hr flag
At least in jq-1.6 there's a `startswith()` boolean, so you could also do something like `.env | to_entries[] | select(.key | startswith("aa_") | not) | ( .key + "=" + (.value | @sh) )`
αғsнιη avatar
cn flag
@steeldriver I didn't think they could have several functions for that aside match(), capture() and test(); interesting! thanks a lot
hr flag
tbh I only figured that out just before you posted - but I hadn't come up with anything as nice as your `( .key + "=" + (.value | @sh) )` for the output formatting
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.