2015-02-05 68 views
1
<ServerCluster CloneSeparatorChange="false" GetDWLMTable="false" IgnoreAffinityRequests="true" LoadBalance="Round Robin" Name="Penguin-CL-Nest-s1" ServerIOTimeoutRetry="-1"> 

用AND运算符表达Sed表达式不起作用,为什么不呢?

我试图做的是一个查找和替换,其中字符串匹配IgnoreAffinityRequests =“true”和企鹅-CL-巢S1或S2,那么当匹配的字符串真正应更换与假。

enterIgnoreAffinityRequests="true" 
Name="Penguin-CL-Nest-s1" 
Name="Penguin-CL-Nest-s2" 

这里是我使用SLES11.3

sed -i -r -e '/IgnoreAffinityRequests="true"/{;/Name="Penguin-CL-Nest-\w\d"/s/IgnoreAffinityRequests="true"/IgnoreAffinityRequests="false"/;}' example1 

它确实没有正则表达式的工作,任何帮助,衷心感谢,感谢命令。

sed -i -e '/IgnoreAffinityRequests="true"/{;/Name="Penguin-CL-Nest-s1"/s/IgnoreAffinityRequests="true"/IgnoreAffinityRequests="false"/;}' example1 

回答

2

编辑XML与战略经济对话不是一个好主意,因为在意想不到的地方或重新排序属性的空格 - 没有人谁使用XML的预期是一个问题 - 可以打破你的脚本。 XML不是基于行的格式,sed是基于行的工具,所以两者不能很好地结合在一起。

相反,我建议您使用正确解析和编辑XML的工具,例如xmlstarlet。在这种情况下:

xmlstarlet ed -u '//ServerCluster[(@Name="Penguin-CL-Nest-s1" or @Name="Penguin-CL-Nest-s2") and @IgnoreAffinityRequests="true"]/@IgnoreAffinityRequests' -v 'false' 

这样做的关键部分是-u后的XPath,其中

  • //ServerCluster是文档中的ServerCluster节点的任何地方,
  • //ServerCluster[condition]/@IgnoreAffinityRequestsServerClusterIgnoreAffinityRequests属性节点中满足condition
  • 条件的任何位置条件(@Name="Penguin-CL-Nest-s1" or @Name="Penguin-CL-Nest-s2") and @IgnoreAffinityRequests="true"如果ServerCluster节点的NameIgnoreAffinityRequests属性满足它,则为true。

这样,xmlstarlet命令将更新相匹配的所有实体此(即,ServerClusterNodesIgnoreAffinityRequests属性其IgnoreAffinityRequests属性是目前真实且其Name属性为Penguin-CL-Nest-s1Penguin-CL-Nest-s2)值为false

+0

然后你可以使用像'sed'/ Name =“Penguin-CL-Nest-s [12]/s/IgnoreAffinityRequests =”true“/ IgnoreAffinityRequests =”false“/''' s/IgnoreAffinityRequests =“true”/ IgnoreAffinityRequests =“false”/'在符合'/ Name =“Penguin-CL-Nest-s [12] /'的行中。基本形式是'/ pattern/command',如果模式空间(行,或者在前面的命令中编辑行的结果)匹配'pattern','command'将被执行。 – Wintermute 2015-02-06 09:58:34

相关问题