如何从YML在shell脚本中移除子块? [英] How to remove a child block from YML in shell script?
问题描述
我想比较两个阳明
文件和删除基于使用shell脚本一定的条件块。我使用这个阳明
解析器, https://开头gist.github.com/pkuczynski/8665367 为阳明
的文件进行比较。但我真的很strugglig很多关于从中除去块。例如,
I'm trying to compare two yml
files and removing a block based on some conditions using shell script. I'm using this YML
parser, https://gist.github.com/pkuczynski/8665367 for the comparison of yml
files. But I'm really strugglig a lot for removing the block from them. For example,
tool:
image: tool.xxx.com/platform/app:dev
log_driver: syslog
restart: always
ports:
- "54325:80"
- "543325:80"
volume:
- "a:b"
tool1:
image: tool1.xxx.com/platform/app:dev
log_driver: syslog
restart: always
ports:
- "54325:80"
- "543325:80"
volume:
- "a:b"
如何删除端口下阻止工具
键,这将使得输出类似,
How to remove ports block under tool
and which will make output similar to,
tool:
image: tool.xxx.com/platform/app:dev
log_driver: syslog
restart: always
volume:
- "a:b"
tool1:
image: tool1.xxx.com/platform/app:dev
log_driver: syslog
restart: always
ports:
- "54325:80"
- "543325:80"
volume:
- "a:b"
我试着用AWK和SED几岁,但我不知道到底,因为它包含涉及多行一些复杂的情况。
I tried somethings using awk and sed, but I don't know exactly as it contains some complex condition involving multiple lines.
任何帮助或建议将是很大的帮助和很多AP preciated。
Any help or suggestion on this would be greatly helpful and much appreciated.
推荐答案
您可以使用此awk命令:
You can use this awk command:
awk '$1 == "tool:"{t=1}
t==1 && $1 == "ports:"{t++; next}
t==2 && /:[[:blank:]]*$/{t=0}
t != 2' file.yml
说明:
- 设置
T = 1
当我们遇到工具:
作为第一列 - 当
T == 1
,使T = 2
当我们遇到第1列端口:
- 重置为
T = 0
在T == 2
,我们得到了与结束的行:
- 打印时每
T = 2
(意味着我们不在行工具 - >端口:
部分)
!
- Set
t=1
when we encountertool:
as first column - When
t==1
, maket=2
when we encounter 1st column inports:
- Reset to
t=0
whent==2
and we get a line that ends with:
- print each line when
t != 2
(means we are not intool: -> ports:
section)
输出:
tool:
image: tool.xxx.com/platform/app:dev
log_driver: syslog
restart: always
volume:
- "a:b"
tool1:
image: tool1.xxx.com/platform/app:dev
log_driver: syslog
restart: always
ports:
- "54325:80"
- "543325:80"
volume:
- "a:b"
这篇关于如何从YML在shell脚本中移除子块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!