Issue
I have a yaml file:
version: xxx
description: xxx
inspec_version: "~> 4"
depends:
- name: yyy
url: random-url
supports:
- platform: aws
How can I remove this from the file?
depends:
- name: yyy
url: random-url
so the file will look like:
version: xxx
description: xxx
inspec_version: "~> 4"
supports:
- platform: aws
The name and url value can be random but it will always be depends: name, url
.
I tried this with sed for the first line but it didn't work:
sed -i '^depends:' test.yml
Solution
Use this (I've removed the -i
, and you can add it back when you're happy with the result):
sed '/^depends:/{N;N;d}' test.yml
Details:
/^depends:/
matches the first of the three lines{…}
goups the commands to be executed when the above pattern matched;
separates successive commandsN
appends the following line to the current pattern space (the line you were editing when the match occurred); this happens twice, resulting in the pattern space holding the line matching the pattern, plus the two following linesd
deletes the pattern space
Answered By - Enlico Answer Checked By - Terry (WPSolving Volunteer)