簡體   English   中英

搜索和替換RANGE模式

[英]Search and Replace RANGE Pattern

想象一下,將多個HTML文件與所有剩余的格式,標記等合並了-不用擔心為什么 -應該使用哪種工具從隨后合並的html文件的開始行中進行搜索,即<!doctype html>...<h1>標頭的開頭? 該范圍模式應改為水平尺。

---END OF PREV MERGED FILE---
---BEGIN SEARCH/REPLACE HERE---
<!doctype html>
        <!--[if !IE]>
        <html class="no-js non-ie" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]-->
        <!--[if IE 7 ]>
        <html class="no-js ie7" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]-->
        <!--[if IE 8 ]>
        <html class="no-js ie8" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]-->
        <!--[if IE 9 ]>
---HEAD,META,ETC---
---END SEARCH/REPLACE HERE---
<h1>TITLE OF NEXT MERGED FILE</h1>

我不確定sedawk是否是錯誤的工具,但是首選類似工具/解決方案。


輸入

<li><strong>email_from = root@localhost</strong>, <strong>email_to = root</strong>, <strong>email_host = localhost</strong> defines respectively when the message is a mail the originator&#8217;s email address, the recipient&#8217;s
 email address and the host to which the mail is sent.<strong><br />
 30658  </strong></li>
 30659  </ul>
 30660  <p>Source: <a title="http://linuxaria.com/howto/enabling-automatic-updates-in-centos-7-and-rhel-7" href="http://linuxaria.com/howto/enabling-automatic-updates-in-centos-7-and-rhel-7">Linuxaria&#8217;s website</a>.</p>
 30661                                                                          </div><!-- end of .post-entry -->

 30662

 30663  <div class="post-edit"></div>
 30664                                                          </div><!-- end of #post-4116 -->
 30665
 30666




 30667          <!doctype html>
 30668          <!--[if !IE]>
 30669          <html class="no-js non-ie" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]-->
 30670          <!--[if IE 7 ]>
 30671          <html class="no-js ie7" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]-->
 30672          <!--[if IE 8 ]>
 30673          <html class="no-js ie8" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]-->
 30674          <!--[if IE 9 ]>
 30675          <html class="no-js ie9" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]-->
 30676          <!--[if gt IE 9]><!-->
 30677  <html class="no-js" lang="en-US" prefix="og: http://ogp.me/ns#"> <!--<![endif]-->
 30678          <head>

 30679                  <meta charset="UTF-8"/>
 30680                  <meta name="viewport" content="width=device-width, initial-scale=1.0">

 30681                  <title>something something</title>

 30682                  <link rel="profile" href="http://gmpg.org/xfn/11"/>
 30683                  <link rel="pingback" href="www.example.com"/>

 30684
 30685          <h1 class="entry-title post-title">Something Something</h1>

預期產量

<li><strong>email_from = root@localhost</strong>, <strong>email_to = root</strong>, <strong>email_host = localhost</strong> defines respectively when the message is a mail the originator&#8217;s email address, the recipient&#8217;s
     email address and the host to which the mail is sent.<strong><br />
     30658  </strong></li>
     30659  </ul>
     30660  <p>Source: <a title="http://linuxaria.com/howto/enabling-automatic-updates-in-centos-7-and-rhel-7" href="http://linuxaria.com/howto/enabling-automatic-updates-in-centos-7-and-rhel-7">Linuxaria&#8217;s website</a>.</p>
     30661                                                                          </div><!-- end of .post-entry -->

     30662

     30663  <div class="post-edit"></div>
     30664                                                          </div><!-- end of #post-4116 -->


    <hr />




     30685          <h1 class="entry-title post-title">Something Something</h1>

這似乎可以滿足您的要求:

awk '/<!doctype html>/{f=1;print "    <hr />";} /<h1 class=/{f=0;} !f' input >output

這個怎么運作

  • /<!doctype html>/{f=1;print " <hr />";}

    當我們到達包含<!doctype html>的行時,這會將標志f設置為1以表示我們應該停止打印。 然后,我們打印水平尺。

  • /<h1 class=/{f=0;}

    當我們到達包含<h1 class=的行時。 將標志f設置為0表示我們可以繼續打印。

  • !f

    如果f0這將導致當前行被打印。

    更詳細地, !f是一個條件 當條件為真時,awk將執行操作。 由於未指定任何操作,awk將執行其默認操作,即打印行。 ! 是awk表示否定的符號。 因此,當f為假(0)時, !f為真,並打印該行。

保留第一個doctype標簽

假設我們要刪除第一個之外的所有文檔類型標簽。 在這種情況下:

awk '/<!doctype html>/{count++; if (count>1){f=1; print "    <hr />";}} /<h1 class=/{f=0;} !f' input

這可以通過添加另一個變量count ,該變量跟蹤我們已經看到了多少個doctype標簽。 僅當我們看到多個文檔類型標記后,標志f才設置為1

為了演示以上內容,讓我們使用此輸入文件:

$ cat input2
miscellaneous stuff
30667          <!doctype html>
30668          something
30669          <h1 class="entry-title post-title">Something Something</h1>
More stuff
30667          <!doctype html>
30668          something 2
30669          <h1 class="entry-title post-title">Something Something</h1>
Still More stuff
30667          <!doctype html>
30668          something 3
30669          <h1 class="entry-title post-title">Something Something</h1>
Stuff at end

該命令產生的輸出為:

$ awk '/<!doctype html>/{count++; if (count>1){f=1; print "    <hr />";}} /<h1 class=/{f=0;} !f' input2
miscellaneous stuff
30667          <!doctype html>
30668          something
30669          <h1 class="entry-title post-title">Something Something</h1>
More stuff
    <hr />
30669          <h1 class="entry-title post-title">Something Something</h1>
Still More stuff
    <hr />
30669          <h1 class="entry-title post-title">Something Something</h1>
Stuff at end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM