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"
I tried somethings using awk and sed, but I don't know exactly as it contains some complex condition involving multiple lines.
Any help or suggestion on this would be greatly helpful and much appreciated.
You can use this awk command:
Explanation:
t=1
when we encountertool:
as first columnt==1
, maket=2
when we encounter 1st column inports:
t=0
whent==2
and we get a line that ends with:
t != 2
(means we are not intool: -> ports:
section)Output: