Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有没有人知道如何替换模式,除非它匹配某些特定模式使用sed?
sed
例如,所有的“ foo”都应该被替换,除了“ food”
foo
food
这可能对您有用(GNU sed):
sed -r ':a;s/foo([^d]|$)/xxx\1/;ta' file
这将替换foo后面的字符,除非它是d.
d
如果您对 sed 的替代方案持开放态度,perl 的环视断言会很有帮助:
$ perl -E '$s="foofoofoodfoofood"; $s=~s/foo(?!d)/bar/g; say $s' barbarfoodbarfood