I'm trying to turn the following:
## Features
feature:foo
feature: baz
into:
## Features
- feature:foo
- feature: baz
I came up with this structure:
sed -i '' 's/^[^#]/- /' $1
but that results in:
## Features
- eature:foo
- eature: baz
FWIW my sed command would have worked fine if I was just trying to add it to the beginning of the line without doing any checks for #
, but when I do a pattern match, it eats through one of the existing characters.
CodePudding user response:
You may use one of these sed
solution:
sed 's/^[^#]/- &/' file
# or
sed '/^#/! s/^/- /' file
## Features
- feature:foo
- feature: baz
To save changes inline on Mac OSX (BSD sed) use:
sed -i '' 's/^[^#]/- &/' file
sed -i '' '/^#/! s/^/- /' file
Note that sed 's/^[^#]/- &/' file
uses &
in replacement to put matched character back in output string so that that character matched by [^#]
is put back in substitution.