I have a file with a yaml data as below:
cat x.yml
foo:
- bar: 1
- zoo: 2
I am able to insert the text but this is messing the indentation(see 2nd line):
sed -r '/^[ ]+- bar:/a- hoo: 3' x.yml
foo:
- bar: 1
- hoo: 3
- zoo: 2
Then, I tried to backreference the leading spaces but seems like it is not working with /a flag.
sed -r '/^([ ]+)- bar:/a\1- hoo: 3' x.yml
foo:
- bar: 1
1- hoo: 3
- zoo: 2
Any help to get the following using a one-liner ?
foo:
- bar: 1
- hoo: 3
- zoo: 2
>Solution :
I suggest to switch to GNU sed’s s command:
sed -E 's/( *)- bar:.*/&\n\1- hoo: 3/' file
Output:
foo: - bar: 1 - hoo: 3 - zoo: 2
See: man sed and The Stack Overflow Regular Expressions FAQ