awksedyaml

Is there a simple awk/sed way to print list in YAML file?


I'm looking for an optimized way to print a particular list in a YAML file using sed or/and awk. For example, in the below sample yaml file, how do I get the list of Fruits alone printed on screen, say, comma-separated?

Input File: boston_mart.yaml

What I am able to achieve using awk is to print after "Fruits:" but I also want another condition to print only if "-" is in front of the words. That is where I am stuck.

## YAML
Market: open
Season: fall
Fruits:
- apple
- orange
- banana
- grapes
Vendors: 7
Buyers: 5
Vegetables:
- tomato
- carrot
- broccoli
Location: Boston

Output

apple
orange
banana
grapes

Solution

  • $ awk '$1 == "-"{ if (key == "Fruits:") print $NF; next } {key=$1}' file
    apple
    orange
    banana
    grapes