2016-07-06 73 views
1

我有我的YAML文件,如下解析YAML文件:我如何从一个Linux shell脚本

report_id: demographics 
columns: 
- date 
- channel_id 
- video_id 
- claimed_status 
- uploader_type 
- live_or_on_demand 
report_id: device_os 
columns: 
- date 
- channel_id 
- video_id 
- asset_id 
- claimed_status 
- uploader_type 
- live_or_on_demand 
- subscribed_status 
- country_code 
- device_type 
- operating_system 

我想使用shell脚本来分析我的YAML文件和想要的答案为:

report_id: demographics 
date 
channel_id 
video_id 
claimed_status 
uploader_type 
live_or_on_demand 
report_id: device_os 
date 
channel_id 
video_id 
asset_id 
claimed_status 
uploader_type 
live_or_on_demand 
subscribed_status 
country_code 
device_type 
operating_system 
+1

这不是一个有效的YAML文件,因为您在顶级映射('columns','report_id')中拥有非唯一键。您也不能缩进第一个'columns',因为这会导致更严重的解析错误,但我猜这是错字。 – Anthon

回答

1
sed '/columns:/d;s/^\s*-\s*//g' file.yml 
+0

我想通过解析yaml文件作为函数来显示report_id中的内容。如下面的代码: – sneha

+0

function parse_yaml(){local prefix = $ 2 local s ='[[:space:]] *'w ='[a-zA-Z0-9 _] *'fs = $(echo @ | tr @'\ 034') sed -ne“s |^\($ s \):| \ 1 |” \ -e“s |^\($ s \)\($ w \)$ s:$ s [\''] \(。* \)[\''] $ s \ $ | \ 1 $ fs \ 2 $ fs \ 3 | p“\ -e”s |^\($ s \)\($ w \)$ s:$ s \(。* \)$ s \ $ | \ $ $ fs \ 2 $ fs \ 3 | p“$ 1 | awk -F $ fs'indent = length($ 1)/ 2;如果(长度($ 3)> 0){ vn =“”; for(i = 0); if(i = 0; i sneha

+0

后来当我回显report_id时,它应该显示我两个report_id。我在代码旁边编写一个case语句,以便在作为变量传递时选择所需的report_id内容。 – sneha