如何在sed中使用数字标志

时间:2016-08-22 03:25:26

标签: sed

我已阅读sed信息。在捕获3.5:s命令

有一个描述:

The s command can be followed by zero or more of the following flags:

number
Only replace the numberth match of the regexp.
Note: the posix standard does not specify what should happen when you mix
the g and number modifiers, and currently there is no widely agreed 
upon meaning  across sed implementations. For GNU sed, the interaction
is  defined to be: ignore matches before the numberth, and then match
and  replace all matches from the numberth on.

我不知道如何使用它,谁可以举一个例子。

echo a1 | sed -n 's/\(a\)1/\13/p'

结果与

没什么不同
echo a1 | sed -n 's/\(a\)1/\13/1p' 

2 个答案:

答案 0 :(得分:1)

试试这个:

echo "hi hi hi" | sed 's/hi/hello/2'
echo "hi hi hi" | sed 's/hi/hello/3'

答案 1 :(得分:0)

当有多个比赛时,这个数字显然才有意义。

sed 's/a/b/4' <<<aaaaa
aaaba

如果没有第四场比赛,显然不会发生替换。