There's something weird when I write that:
我这么写的时候有些奇怪:
echo "VAL LES LE VAL LE VAL" | sed -E -e 's/ LES? / /g'
I get:
我得到:
VAL LE VAL VAL
instead of:
而不是:
VAL VAL VAL
The word LE after the LES one is kept, but the last LE word is well removed.
单词LE在LES one后面被保留,但是最后一个LE word被删除得很好。
I use the sed command on Mac OS X 10.8 which dates of May 10, 2005
我在Mac OS X 10.8上使用sed命令,日期是2005年5月10日
Does anyone know the reason of this result?
有人知道这个结果的原因吗?
Thanks.
谢谢。
2 个解决方案
#1
2
sed does not recursively match output patterns. That is, the input line is read, and when a pattern is matched, the modified output is generated, and the rest of the input line is scanned. The "s" command will not scan the newly created output. Thus, after you replace the first LES
with , the newly created space will not be scanned, and the next
LE
will not be matched since there is no leading space.
sed并不递归地匹配输出模式。也就是说,读取输入行,当匹配模式时,生成修改后的输出,扫描其余的输入行。“s”命令不会扫描新创建的输出。因此,当你用第一个LES替换后,新创建的空间将不会被扫描,而下一个LE将不会被匹配,因为没有前导空间。
Alternative solution:
可选择的解决方案:
echo "VAL LES LE VAL LE VAL" | sed -E -e 's/ *LES?//g'
#2
0
echo "VAL LES LE VAL LE VAL" | sed -E -e 's/ LES? / /g'
Here <space>LES<space>
matched and got replaced by <space>
. so the next char to start with will be LE<space>
which doesnt match the sed expr <space>LES?<space>
and hence was unmodified.
这里
#1
2
sed does not recursively match output patterns. That is, the input line is read, and when a pattern is matched, the modified output is generated, and the rest of the input line is scanned. The "s" command will not scan the newly created output. Thus, after you replace the first LES
with , the newly created space will not be scanned, and the next
LE
will not be matched since there is no leading space.
sed并不递归地匹配输出模式。也就是说,读取输入行,当匹配模式时,生成修改后的输出,扫描其余的输入行。“s”命令不会扫描新创建的输出。因此,当你用第一个LES替换后,新创建的空间将不会被扫描,而下一个LE将不会被匹配,因为没有前导空间。
Alternative solution:
可选择的解决方案:
echo "VAL LES LE VAL LE VAL" | sed -E -e 's/ *LES?//g'
#2
0
echo "VAL LES LE VAL LE VAL" | sed -E -e 's/ LES? / /g'
Here <space>LES<space>
matched and got replaced by <space>
. so the next char to start with will be LE<space>
which doesnt match the sed expr <space>LES?<space>
and hence was unmodified.
这里