shell的随记(十 二)

一、一段文本保留前三行是表头,想保留从第四行开始匹配compute, 匹配了打印。

在这里插入图片描述

awk 'NR==1,NR==3;/compute/{print $0}' 1.txt
awk 'NR<4||/compute/{print $0}' 1.txt
awk 'NR<4;/compute/{print $0}' 1.txt

二、取消tomcat的server.xml的注释模块

被 <!--   -->  包裹的模块
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
       

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
         <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->
</Host>
#六子大佬的解法:
awk 'BEGIN{p=1}/<!--/{p=0}/-->/{p=1;next}p' server.xml.bak

我做了多行匹配,但是grep 加上-v匹配是空,可能是写法+匹配多行的缘故,如果是<!-- -->是一行,则是可以取反的。

#有问题 ,没加-v 都匹配出了注释模块里面的东西
grep -zPo -v  "(?s)<\!--.*?-->(?=\n)" server.xml.bak

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Nightwish5/article/details/113828410