This is what I'm doing (simplified example):
gsed -i -E 's/^(?!foo)(.*)$/bar\1/' file.txt
I'm trying to put bar
in front of every line that doesn't start with foo
. This is the error:
gsed: -e expression #1, char 22: Invalid preceding regular expression
What's wrong?
Note that SED doesn't have lookahead and therefore doesn't support the regex feature you were trying to use. It can be done in SED using other features, as others have mentioned.
Lookahead assertions are part of JavaScript's original regular expression support and are thus supported in all browsers.
Lookahead is used as an assertion in Python regular expressions to determine success or failure whether the pattern is ahead i.e to the right of the parser's current position. They don't match anything. Hence, they are called as zero-width assertions.
I created a test using grep but it does not work in sed . This works correctly by returning bar . I was expecting footest as output, but it did not work. sed does not support lookaround assertions.
sed -i '/^foo/! s/^/bar/' file.txt
-i
change the file in place/^foo/!
only perform the next action on lines not !
starting with foo ^foo
s/^/bar/
change the start of the line to bar If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With