本文介绍了使用sed删除xml中包含某些文本的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的xml.
I have an xml that looks like following.
<rootNode>
<appender name="SERVER_FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/COM_FIND.log"/>
<param name="Threshold" value="INFO"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<appender name="CAT_FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/COM_Now.log"/>
<param name="Threshold" value="INFO"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="MaxBackupIndex" value="2"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<appender name="SERVER_FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/COM_A.log"/>
<param name="Threshold" value="INFO"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<appender name="SERVER_FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/COM_Find.log"/>
<param name="Threshold" value="INFO"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<logger name="com.mycompany">
<level value="INFO"/>
<appender-ref ref="SERVER_FILE"/>
</logger>
</rootNode>
是.它摘自log4j xml.我基本上想做的是使用sed命令删除所有包含单词'COM_Find.log'的附加标签/元素.我到目前为止所拥有的是:-
Yes.Its an excerpt from the log4j xml. What i basically want to do is remove all the appender tags/elements (from )that contain the word 'COM_Find.log' using the sed command.What i have so far is this :-
sed -e '/<appender/,/<\/appender>/{H;$!d;}' -e 'x;/COM_Find.log/d' file.xml
从第一个附加程序到最后一个附加程序进行贪婪搜索,然后删除中间的所有内容.我只希望删除那些特定的附加标签/元素(来自).
Which does a greedy search from the first appender to the last and deletes everything in between. I just want only those specific appender tags/elements(from ) to be removed.
输出应为:-
<rootNode>
<appender name="CAT_FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/COM_Now.log"/>
<param name="Threshold" value="INFO"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="MaxBackupIndex" value="2"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<appender name="SERVER_FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/COM_A.log"/>
<param name="Threshold" value="INFO"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<logger name="com.mycompany">
<level value="INFO"/>
<appender-ref ref="SERVER_FILE"/>
</logger>
</rootNode>
有人可以帮助我吗?
推荐答案
sed -ne '/rootNode/{ p; b }; /<appender /{ h; b }; /<logger /{ h; b }; H; /<\/logger>/{ x; p; b; }; /<\/appender>/{ x; /COM_Find[.]log/b; /COM_FIND[.]log/b; p }' file
输出:
<rootNode>
<appender name="CAT_FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/COM_Now.log"/>
<param name="Threshold" value="INFO"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="MaxBackupIndex" value="2"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<appender name="SERVER_FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/COM_A.log"/>
<param name="Threshold" value="INFO"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<logger name="com.mycompany">
<level value="INFO"/>
<appender-ref ref="SERVER_FILE"/>
</logger>
</rootNode>
另一个awk:
awk '{ p = 1 } /<appender /{ t = $0; while (getline > 0) { t = t ORS $0; if (/COM_FIND[.]log/ || /COM_Find[.]log/) p = 0; if (/<\/appender>/) break } $0 = t } p' file
或带有IGNORECASE
的GNU awk:
Or GNU awk with IGNORECASE
:
gawk -v IGNORECASE=1 '{ p = 1 } /<appender /{ t = $0; while (getline > 0) { t = t ORS $0; if (/COM_FIND[.]log/) p = 0; if (/<\/appender>/) break } $0 = t } p' file
这篇关于使用sed删除xml中包含某些文本的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!