我有一个看似跟随的xml。
<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的摘录。我基本上想要做的是删除包含单词&#39; COM_Find.log&#39;的所有appender标签/元素(from)。使用sed命令。 到目前为止我所拥有的是: -
sed -e '/<appender/,/<\/appender>/{H;$!d;}' -e 'x;/COM_Find.log/d' file.xml
从第一个appender到最后一个的贪婪搜索是什么,并删除它们之间的所有内容。我只想删除那些特定的appender标签/元素(from)。
输出应为: -
<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>
有人可以帮助我吗?
答案 0 :(得分:1)
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:
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
答案 1 :(得分:0)
我不明白你想删掉整条线吗?
如果是,你可以grep
:
grep -v "COM_Find.log" yourfile.xml > newfile.xml