【问题标题】:search and replace in a sample file在示例文件中搜索和替换
【发布时间】:2012-02-25 01:29:14
【问题描述】:

您好,我有以下文件

      <strong>Ramandand Sagar Krishna part 34</strong> Vasudev comes back 
and girl disappears from Kansa's hand and the first temple she instructs Devs to make at Vindhyachal <a href="http://www.dailymotion.com/embed/video/x3p3gu?
width=320&#038;theme=none&#038;wmode=transparent">http://www.dailymotion.com/embed/video/x3p3gu?width=320&#038;theme=none&#038;wmode=transparent</a> <a 
href="http://www.dailymotion.com/video/x3p3gu_krishna-part-34_shortfilms" 
target="_blank">Krishna Part 34</a> <strong>Ramandand Sagar Krishna part 35</strong> Celebrations at Yashoda's house and Vasudev Devki freed from jail <a href="http://www.dailymotion.com/embed/video/x3p3sg?width=320&#038;theme=none&#038;wmode=transparent">
http://www.dailymotion.com/embed/video/x3p3sg?width=320&#038;theme=none&#038;wmode=transparent</a> <a href="http://www.dailymotion.com/video/x3p3sg_krishna-part-35_shortfilms" target="_blank">Krishna Part 35</a> <a href="http://www.dailymotion.com/video/x66a71_krishna-143_shortfilms" target="_blank">Krishna 143</a></em></div>

在上面的文件中我想替换

任何以下类型的 HTML

<a href="http://www.dailymotion.com/embed/video/x5ftx3?width=320">http://www.dailymotion.com/embed/video/x5ftx3?width=320</a>

关键字是任何具有wmode=transparentwidth=320 的HTML 标记应替换为空格。有没有简单的方法可以做到这一点?有很多HTML 标记,例如 &lt;a href=""&gt; &lt;/a&gt; 在他们的行中没有 wmode=transparent。 上面发布的文件非常大,HTML 中有大约 30K 行,所以我只发布了相关行。 我在 Ubuntu 系统上。

【问题讨论】:

  • 没有简单的方法可以使用 sed 可靠地做到这一点,因为使用正则表达式的 parsing HTML 不是一个好主意。
  • 如果您将鼠标悬停在您分配给问题的标签上,您会注意到,在最坏的情况下,search-and-replace 有 3 个关注者。我敢打赌 html 的追随者比这还多。当然,目标是让尽可能多的知识渊博的人看到您的问题。祝你好运!

标签: html search sed awk find


【解决方案1】:

这里是link,您可以在其中找到问题的答案。

在您的情况下,您必须为 sed 创建一个脚本文件,例如

s/wmode=透明//g
s/width=320//g

并运行类似的东西:

sed -f replace_file in.txt > out.txt

希望对你有帮助。

祝你有美好的一天

【讨论】:

  • 这并不能解决问题。他想识别具有 wmode=transparent 或 width=320 的标签,然后删除整个标签,而不仅仅是那些部分。由于不能保证每个标签都在自己的行上,sed 特别不合适。
【解决方案2】:

正如 Sorpigal 所指出的,没有简单的答案可以解决这个问题。如果你愿意破坏你的行尾,你可以试试我丑陋的混合物。它可能会对您有所帮助:

cat file.txt | tr -d "\n" | awk '{ for (i=1; i&lt;=NF; i++) if ($i !~ /wmode=transparent|width=320/) printf "%s ", $i} END {print ""}' file.txt | sed -e "s%&lt;a &lt;a%&lt;a%g"

输出:

&lt;strong&gt;Ramandand Sagar Krishna part 34&lt;/strong&gt; Vasudev comes back and girl disappears from Kansa's hand and the first temple she instructs Devs to make at Vindhyachal &lt;a href="http://www.dailymotion.com/embed/video/x3p3gu? &lt;a href="http://www.dailymotion.com/video/x3p3gu_krishna-part-34_shortfilms" target="_blank"&gt;Krishna Part 34&lt;/a&gt; &lt;strong&gt;Ramandand Sagar Krishna part 35&lt;/strong&gt; Celebrations at Yashoda's house and Vasudev Devki freed from jail &lt;a href="http://www.dailymotion.com/video/x3p3sg_krishna-part-35_shortfilms" target="_blank"&gt;Krishna Part 35&lt;/a&gt; &lt;a href="http://www.dailymotion.com/video/x66a71_krishna-143_shortfilms" target="_blank"&gt;Krishna 143&lt;/a&gt;&lt;/em&gt;&lt;/div&gt;

我敢肯定,这种单线可以通过某种方式进行改进。如果您确实觉得这很有用,那么您可能希望在边界上拆分输出以整理内容。 Sed 可以很好地解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2014-06-01
    • 2015-08-20
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多