关于sed替换命令
用正则表达式b*匹配0或任意个b
期望删除原文中的bb输出ac
然而输出是abbc
答案找到了+_+
(2) The '*' metacharacter represents zero or more instances of the previous expression. The '*' metacharacter looks for the leftmost possible match first and will match zero characters. Thus,
echo foo | sed 's/o*/EEE/'
will generate 'EEEfoo', not 'fEEE' as one might expect. This is because /o*/ matches the null string at the beginning of the word.
After finding the leftmost possible match, the '*' is GREEDY; it always tries to match the longest possible string. When two or three instances of '.*' occur in the same RE, the leftmost instance will grab the most characters.
-n表示只打印匹配行
*表示前面的字符的任意次出现
p表示打印匹配行
abbc里面含有b,所以肯定匹配所有行,abbc就是一行呀,有问题吗?
如有帮助,请采纳一下谢谢
b*的第一个匹配项是最左边的空字符串
改成
echo 'abbc' | sed -n 's/b*//2p'
就和期望一致了