为什么sed删除大于符号

Why is sed removing the greater than symbol

当我 运行 我的 sed 命令时,小于号被删除。

a::b<type::value> & d
a::b<ns::type::value>& d
sed -i 's/[^:]type/changed::type/g'

结果

a::bchanged::type::value> & d
a::b<ns::type::value>& d

我希望实际得到以下信息:

a::b<changed::type::value> & d
a::b<ns::type::value>& d

为什么去掉了小于号?我怎样才能保留它?

该符号被删除,因为它匹配 [^:] "not a colon",作为要替换的字符串的一部分。您可以通过捕获它并将其放回原处来绕过它:

sed -i 's/\([^:]\)type/changed::type/g'