Linux 匹配模板2次替换时如何插入sed? echo'

Linux 匹配模板2次替换时如何插入sed? echo',linux,bash,shell,sed,Linux,Bash,Shell,Sed,您可以使用: </ns:epsIndividualDefaultContextId><ns:epsIndividualContextId>100<ns:epsIndividualContextId></ns:epsIndividualContextId><ns:epsIndividualContextId></ns:epsIndividualContextId> 这将在整个字符串中找到字符串的次数与在sed语句末尾使用“g

您可以使用:

</ns:epsIndividualDefaultContextId><ns:epsIndividualContextId>100<ns:epsIndividualContextId></ns:epsIndividualContextId><ns:epsIndividualContextId></ns:epsIndividualContextId>

这将在整个字符串中找到字符串的次数与在sed语句末尾使用“g”替换所有匹配的出现次数相同。但要多次替换同一字符串,必须使用sed,次数如下所示

两次更换

echo whole_string | sed "s/string_to_find/string_to_insert/g"

echo'我听说在某些版本上,您可以执行
s/pattern/replace/fromOccurence,toOccurence
,但这在我的GNU上不起作用。一种简单的方法是将第一次出现的内容替换两次,即
echo$data | sed's/pattern/replace'| sed's/pattern/replace/'
@Aaron您可以使用
sed-e EXPR-e EXPR
而不是管道传输两个命令。请使用格式整齐的示例输入发布预期输出,在unix.stackexchange上有一个答案可能会有所帮助:您想做什么?请您的问题使用更简单的示例,例如,对于regexp和替换字符串,使用
a
b
,这样您想要做的事情就不会隐藏在数百个不相关的字符中。每个人的答案都被否决和删除了,因为很难在你的问题中看到你真正想要做什么。如果我需要添加100次?100个处方模式?)然后使用awk代替SEAND,如果我需要添加100次,可以使用awk吗?100种处方模式?)
</ns:epsIndividualDefaultContextId><ns:epsIndividualContextId>100<ns:epsIndividualContextId></ns:epsIndividualContextId><ns:epsIndividualContextId></ns:epsIndividualContextId>
echo whole_string | sed "s/string_to_find/string_to_insert/g"
echo'</ns:epsIndividualDefaultContextId<ns:epsIndividualContextId>100</ns:epsIndividualContextId>' | sed's/<\/ns:epsIndividualContextId>/<ns:epsIndividualContextId<\/ns:epsIndividualContextId>/' | sed's/<\/ns:epsIndividualContextId>/<ns:epsIndividualContextId<\/ns:epsIndividualContextId>/'