问题
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?
回答1:
As far as I know sed has not neither look-ahead nor look-behind. Switch to a more powerful language with similar syntax, like perl.
回答2:
sed -i '/^foo/! s/^/bar/' file.txt
-ichange the file in place/^foo/!only perform the next action on lines not!starting with foo^foos/^/bar/change the start of the line to bar
回答3:
You use perl compatible regular expression (PCRE) syntax which is not supported by GNU sed. You should rewrite your regex according to SED Regular-Expressions or use perl instead.
来源:https://stackoverflow.com/questions/12176026/whats-wrong-with-my-lookahead-regex-in-gnu-sed