【问题标题】:How to find and replace these HTML sentences using grep and perl/sed如何使用 grep 和 perl/sed 查找和替换这些 HTML 句子
【发布时间】:2014-05-12 17:32:13
【问题描述】:

我有几行包含换行符和内容的 HTML,我想替换这样的句子:

<li class="menu-581"><a href="../you-your-me-nm.html" title="You &amp;amp;
">You &amp; Your Service Dog</a></li>

注意"&gt;You之前的换行符

我有第一部分,寻找 sn-p

pcregrep  -r -M -l '<li class="menu-581">(.|\n)*?</li>' *

现在,我想将它输入 Perl 以用新文本替换相同的正则表达式

我正在尝试这个 perl sn-p 但它不起作用(我将它保存在一个名为 test.txt 的文件中):

ls test.* | xargs perl -pe 's/<li class="menu-581">(.|\n)*<\/li>/new/' -pi

有什么线索吗?

【问题讨论】:

  • 像往常一样:不要在 html 上使用正则表达式。使用 DOM 解析器。它会让你的生活更轻松。
  • @MarcB 它是一次性任务,所以没关系
  • @Naughty.Coder: 不,做错事是绝对不行的
  • 如果您正在寻找 perl 解决方案,为什么要使用 sed 标记它?

标签: regex perl sed grep


【解决方案1】:

编辑:这是我实际测试过的,所以它按预期工作:

 ls test.* | xargs perl -pe 'BEGIN{undef $/;};s/(<li class="menu-581">)(.*)(<\/li>)/$1$3/gs'

【讨论】:

    【解决方案2】:
    cat -e test.html
    
    <p>$
    <li class="menu-581"><a href="../you-your-me-nm.html" title="You &amp;amp;$
    ">You &amp; Your Service Dog</a></li>$
    </p>$
    

    然后

    perl -0777 -pe 's{\Q<li class="menu-581"><a href="../you-your-me-nm.html" title="You &amp;amp;
    ">You &amp; Your Service Dog</a></li>}{}g' test.html
    
    <p>
    
    </p>
    

    添加-i 标志以就地编辑:

    perl -0777 -i -pe '...' test*
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-11
      • 2011-09-04
      • 1970-01-01
      • 2020-08-05
      • 2021-12-24
      • 2019-11-29
      • 1970-01-01
      • 2020-11-15
      相关资源
      最近更新 更多