是否有一种简单的 awk/sed 方法可以在 YAML 文件中打印列表? [英] Is there a simple awk/sed way to print list in YAML file?

查看:19
本文介绍了是否有一种简单的 awk/sed 方法可以在 YAML 文件中打印列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用 sed 或/和 awk 在 YAML 文件中打印特定列表的优化方法.例如,在下面的示例 yaml 文件中,我如何在屏幕上单独打印水果列表,比如逗号分隔?

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?

我能够使用 awk 实现的是在Fruits:"之后打印.但我还希望仅在-"时打印另一个条件.是在前面的话.那就是我卡住的地方.任何帮助或指示都会非常有帮助.

What I am able to acheive using awk is to print after "Fruits:" but I also want another contition to print only if "-" is in front of the words. That is where I am stuck. Any help or pointers will be very helpful.

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

输出

apple
orange
banana
grapes

推荐答案

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

这篇关于是否有一种简单的 awk/sed 方法可以在 YAML 文件中打印列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆