I have a YAML file that I need to iterate and return a specific value for a variable, I am unable to use jq so the only available tool I have is bash. The YAML file looks like this:
abc:
NAME: Bob
OCCUPATION: Technician
def:
NAME: Jane
OCCUPATION: Engineer
YAML keys are known and will be iterated over, but say I want to get key abc OCCUPATION value of TECHNICIAN, through googling I managed to construct an awk statement that gives what I want, but it seems rather convoluted. Is there a more elegant way to write this out?
Current implementation is
> awk 'BEGIN{OFS=""} /^[^ ]/{ f=/^abc:/; next } f{ if (sub(/:$/,"")) abc=$2; else print abc,$1 $2}' test.yml| grep "OCCUPATION:" | cut -d':' -f2
Technician
If I could use jq it would have been much simpler but alas, I am stuck with base bash!