Using sed to remove a block of text

前端 未结 3 1866
臣服心动
臣服心动 2021-02-05 02:09

I have a block of text that looks like this:

    
... a bunch of stuff 
    

I\'d like to remov

相关标签:
3条回答
  • 2021-02-05 02:31

    To remove all the text starting from and including <!-- BOF CLEAN --> and ending at and including <!-- EOF CLEAN -->, use following sed command:

    sed -i '/<!-- BOF CLEAN -->/,/<!-- EOF CLEAN -->/d' file_name;
    

    Reference: Delete text or paragraph between two sections using sed

    0 讨论(0)
  • 2021-02-05 02:41
    $ cat text 
    abc
        <!-- BOF CLEAN -->
    ... a bunch of stuff
        <!-- EOF CLEAN -->
    def
    $ sed '/<!-- BOF CLEAN -->/,/<!-- EOF CLEAN -->/d' text 
    abc
    def
    

    http://www.catonmat.net/blog/sed-one-liners-explained-part-three/

    0 讨论(0)
  • 2021-02-05 02:41

    These days I am using the /s modifier to do this. I noticed no one mentioned that. I use markup with is free of spaces too like

    {bof-nf} ... a bunch of stuff {eof-nf}

    So for example, to remove this block, use

    $newcontent = preg_replace("/\{bof-nf\}(.*)\{eof-nf\}\\n/s", "", $newcontent);

    To keep the block but remove the tags, use

    $newcontent = preg_replace("/\{bof-nf\}.*\\n/", "", $newcontent); $newcontent = preg_replace("/\{eof-nf\}.*\\n/", "", $newcontent);

    0 讨论(0)
提交回复
热议问题