使用sed或awk或任何命令保持每个多行匹配

时间:2017-09-28 10:40:35

标签: awk sed grep

我有一个分布在多行的模式,有很多这样的模式。我需要对发现的每个这样的多线模式匹配进行进一步处理。

下面的命令就是我正在尝试的那个:

 <asp:ScriptManager runat="server" ID="ScriptManager2" />
.....
     <asp:Panel ID="pnlRegistrationPhase6" runat="server">
         <div class="modal fade " id="registerStep6" role="dialog">
             <div class="modal-dialog modal-lg">
                 <!-- Modal content-->
                 <div class="modal-content">
                     <asp:UpdatePanel runat="server" ID="updRegistrationStep6">
                         <ContentTemplate>
                             <div class="modal-body ac-modal-success p-a0">
                                 <div class="row">
                                     <div class="col-lg-12">
                                         <div class="col-lg-4 col-md-4 p-l0 hidden-sm hidden-xs img-content">
                                             <img src="images/form_registrasi-14-14.jpg" class="img img-responsive btn-block">
                                         </div>
                                         <div class="col-xs-12 pt-15 pb-10 m-b20 hidden-lg hidden-md heading-reg6">
                                             <button type="button" class="close button-close-cust" data-dismiss="modal">X</button>
                                             <h4 class="text-strong m-t5">Certification</h4>
                                             <ul>
                                                 <li>Maximum 3 (three) latest certifications which sorted from the last, required if already have certifications</li>
                                             </ul>
                                         </div>
                                         <div class="col-lg-8 col-md-8 col-lg-offset-4 col-md-offset-4 form-content">
                                             <div class="col-lg-12 p-r m-b10">
                                                 <button type="button" class="close button-close-cust" data-dismiss="modal">X</button>
                                             </div>
                                             <div class="col-lg-12 mt-20">
                                                 <div class="row form-group">
                                                     <div class="col-lg-9 col-lg-offset-3">
                                                         <p class="text-primary text-strong m-b0">Do you have Certification?</p>
                                                         <span>(Internship or Final Paper isn't considered as Certification)</span>
                                                     </div>
                                                 </div>
                                             </div>
                                             <div class="col-lg-12">
                                                 <div class="row form-group">
                                                     <div class="col-lg-9 col-lg-offset-3">
                                                         <div class="radio radio-primary radio-inline">
                                                             <asp:RadioButton runat="server" ID="yescertification" ClientIDMode="Static" OnClick="showStep6();" GroupName="certification" />
                                                             <label for="yescertification">Yes</label>
                                                         </div>
                                                         <div class="radio radio-primary radio-inline">
                                                             <asp:RadioButton runat="server" ID="nocertification" ClientIDMode="Static" OnClick="hideStep6();" GroupName="certification" />
                                                             <label for="nocertification">No</label>
                                                         </div>
                                                     </div>
                                                 </div>
                                             </div>
                                             <div id="step6Content" class="col-lg-12 no-padding hide">
                                                 <div class="col-lg-12">
                                                     <div class="row form-group">
                                                         <label class="col-lg-3 control-label text-strong text-right">Name of Certification<span class="text-danger">*</span></label>
                                                         <div class="col-lg-6">
                                                             <span class="has-float-label">
                                                                 <asp:TextBox runat="server" ID="txtCertificationName" ClientIDMode="Static" placeholder=" " CssClass="form-control form-flat" ValidationGroup="txtCertificationName" />
                                                                 <label class="text-small" for="certname">Certification Name</label>
                                                             </span>
                                                             <span class="text-danger text-small" style="color: #D64541">
                                                                 <asp:RequiredFieldValidator ErrorMessage="Please Insert Certification Name" ControlToValidate="txtCertificationName" runat="server" ValidationGroup="txtCertificationName" />
                                                             </span>
                                                         </div>
                                                     </div>
                                                 </div>
                                                 <div class="col-lg-12">
                                                     <div class="row form-group">
                                                         <label class="col-lg-3 control-label text-strong text-right">Institution/Company<span class="text-danger">*</span></label>
                                                         <div class="col-lg-6">
                                                             <span class="has-float-label">
                                                                 <asp:TextBox runat="server" ID="txtCertificationInstitution" ClientIDMode="Static" placeholder=" " CssClass="form-control form-flat" ValidationGroup="txtCertificationInstitution" />
                                                                 <label class="text-small" for="instname">Institution Name</label>
                                                             </span>
                                                             <span class="text-danger text-small" style="color: #D64541">
                                                                 <asp:RequiredFieldValidator ErrorMessage="Please Insert Certification Institution/Company" ControlToValidate="txtCertificationInstitution" runat="server" ValidationGroup="txtCertificationInstitution" />
                                                             </span>
                                                         </div>
                                                     </div>
                                                 </div>
                                                 <div class="col-lg-12">
                                                     <div class="row form-group">
                                                         <label class="col-lg-3 control-label text-strong text-right">Year of Certification<span class="text-danger">*</span></label>
                                                         <div class="col-lg-2">
                                                             <div class="form-group has-float-label">
                                                                 <asp:DropDownList runat="server" ID="ddlYearCertification" CssClass="form-control form-flat minimal" ValidationGroup="ddlYearCertification" Width="100%" OnChange="removeTextddlYearCertification();" ClientIDMode="Static">
                                                                 </asp:DropDownList>
                                                                 <span class="text-danger text-small" style="color: #D64541">
                                                                     <asp:RequiredFieldValidator ID="rfvddlYearCertification" ErrorMessage="Please Insert Year" ControlToValidate="ddlYearCertification" runat="server" Display="Dynamic" ValidationGroup="ddlYearCertification" InitialValue="0" />
                                                                 </span>
                                                                 <!--<label for="certyear">Combo Box</label>-->
                                                             </div>
                                                         </div>
                                                     </div>
                                                 </div>
                                                 <div class="col-lg-12">
                                                     <div class="row form-group">
                                                         <label class="col-lg-3 control-label text-strong text-right">Upload Certificate</label>
                                                         <div class="col-lg-9">
                                                             <div class="media">
                                                                 <div class="media-left media-middle">
                                                                     <div class="box-upload">
                                                                         <span class="btn btn-primary btn-pill">
                                                                             <asp:FileUpload runat="server" ID="FileUploadCertificate" CssClass="hidden" />
                                                                             <label for="file" class="m-b0">Choose File</label>
                                                                         </span>
                                                                     </div>
                                                                 </div>
                                                                 <div class="media-body media-middle">
                                                                     <span class="text-danger text-small" style="color: #D64541;">There was an error uploading images.
                                                            <br>
                                                                         Please check and try again.</span>
                                                                 </div>
                                                             </div>
                                                         </div>
                                                     </div>
                                                 </div>
                                                 <div class="col-lg-12">
                                                     <div class="row form-group">
                                                         <div class="col-lg-9 col-lg-offset-3">
                                                             <asp:Button Text="Save" runat="server" CssClass="btn btn-primary btn-pill btn-modal-width" ID="btnSaveCertification" CausesValidation="true" OnClick="btnSaveCertification_Click " />
                                                             <asp:Button Text="Clear" runat="server" CssClass="btn btn-primary btn-pill m-l20 btn-modal-width" ID="btnClearCertification" OnClick="btnClearCertification_Click" />
                                                             <%--                                                                        <button class="btn btn-primary btn-pill btn-modal-width">Save</button>
                                                                    <button class="btn btn-primary btn-pill m-l20 btn-modal-width">Clear</button>--%>
                                                         </div>
                                                     </div>
                                                 </div>
                                                 <div class="col-lg-12">
                                                     <div class="row form-group">
                                                         <div class="col-lg-9 col-lg-offset-3">
                                                         </div>
                                                     </div>
                                                 </div>
                                             </div>

                                             <div class="col-lg-12 m-t20">
                                                 <div class="row form-group m-t20">
                                                     <div class="col-lg-8 col-lg-offset-3">
                                                         <button type="button" class="btn btn-primary btn-pill btn-modal-width" data-toggle="modal" data-target="#registerStep5" data-dismiss="modal">Previous</button>
                                                         <button type="button" class="btn btn-default btn-pill m-l20 btn-modal-width" data-toggle="modal" data-target="#registerStep7" data-dismiss="modal">Next</button>
                                                     </div>
                                                 </div>
                                             </div>
                                         </div>
                                     </div>
                                 </div>
                             </div>
                         </ContentTemplate>
                         <Triggers>
                             <asp:AsyncPostBackTrigger ControlID="btnClearCertification" />
                             <asp:AsyncPostBackTrigger ControlID="btnSaveCertification" />
                         </Triggers>
                     </asp:UpdatePanel>
                 </div>
             </div>
         </div>
     </asp:Panel>

这给出了所有的比赛。我想操纵每一个和进一步的过程。

我的文件如下:

纯延迟路径#308,

0.003 0.003 0.032 0.003(F)0.003 af mclk

0.019 0.022 0.006 0.001(F)0.022 af mclk

结束路径

纯延迟路径#309,

0.003 0.003 0.032 0.003(F)0.003 af mclk

0.019 0.022 0.006 0.001(F)0.022 af mclk

结束路径

这只是为了解释。然后,我需要在每个mutli行匹配中添加所有第一列,然后将其与#id一起打印

没有找到如何处理这个问题。非常感谢任何帮助!!!

2 个答案:

答案 0 :(得分:1)

GNU awk 解决方案,考虑到以下因素:

  • id可能不止一次出现。所有价值$ 1将相加。
  • id可能不是按顺序排列的,所以你可能会发现一些&#34;漏洞&#34;介于两者之间。
  • ID应按顺序打印。

脚本:

$ cat tst.awk
match($0, /Pure delay path #([0-9]+)/, arr){
    id=arr[1]                                          # save id
    low=(low && low<id?low:id); high=(high<id?id:high) # save lowest/highest id
    next
}
id { a[id]=a[id]+$1 }                                  # sum up $1 for id
/END OF PATH/{ id=0 }
END{
    for (i=low; i<=high; i++)
        if (i in a)                                    # check if id is defined
            printf "id: #%s sum: %*.3f\n", i, length(a[i]), a[i]
}

测试数据:

$ cat input.txt

Pure delay path #333,
0.003 0.003 0.032 0.003 (F) 0.003 af mclk
123.019 0.022 0.006 0.001 (F) 0.022 af mclk
END OF PATH

Pure delay path #309,
0.003 0.003 0.032 0.003 (F) 0.003 af mclk
0.019 0.022 0.006 0.001 (F) 0.022 af mclk
END OF PATH

Pure delay path #308 ,
0.003 0.003 0.032 0.003 (F) 0.003 af mclk
0.019 0.022 0.006 0.001 (F) 0.022 af mclk
END OF PATH
Pure delay path #309,
0.003 0.003 0.032 0.003 (F) 0.003 af mclk
123.019 0.022 0.006 0.001 (F) 0.022 af mclk
END OF PATH

结果:

 $ awk -f tst.awk input.txt
 id: #308 sum: 0.022
 id: #309 sum: 123.044
 id: #333 sum: 123.022

如果您不考虑上述3个注意事项,那么脚本可以更加简单,如下所示:

$ cat tst2.awk
match($0, /Pure delay path #([0-9]+)/, arr){
    id=arr[1]
    next
}
id { a[id]=a[id]+$1 }
/END OF PATH/{
    printf "id: #%s sum: %*.3f\n", id, length(a[id]), a[id]
}

答案 1 :(得分:0)

这可能适合你(GNU sed):

sed -n '/Pure delay/I{:a;N;/END OF PATH/I!ba;s/match/replace/gp}' file

使用循环:a;N;/match/!ba收集所需的行,然后以下任何内容都可以处理收集的行,即s/match/replacement/gp

N.B。最后的命令应该是pP命令/标志,否则不会打印任何内容。