搜索和替换
您好我有以下文件搜索和替换
<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&theme=none&wmode=transparent">http://www.dailymotion.com/embed/video/x3p3gu?width=320&theme=none&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&theme=none&wmode=transparent">
http://www.dailymotion.com/embed/video/x3p3sg?width=320&theme=none&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=transparent
或width=320
的HTML标签都应替换为空格。是否有简单的方法可以这样做?有许多HTML标签,如 <a href=""> </a>
其中没有wmode=transparent
。 上面发布的文件非常大,大约有30K行在HTML中,所以我只发布了相关行。 我在Ubuntu系统上。
由于Sorpigal指出,有没有简单的答案来解决这个问题。如果你愿意摧毁你的线路结局,你可以尝试我的丑陋混合物。它可以帮助你:
cat file.txt | tr -d "\n" | awk '{ for (i=1; i<=NF; i++) if ($i !~ /wmode=transparent|width=320/) printf "%s ", $i} END {print ""}' file.txt | sed -e "s%<a <a%<a%g"
输出:
<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? <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/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>
我敢肯定,这一个班轮可能以某种方式加以改进。如果你觉得这很有用,那么你可能想要将边界上的输出拆分成整齐的东西。 Sed
可以为此做好。
有没有简单的方法来做到这一点与sed可靠,因为[解析HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/ 1732454#1732454)与正则表达式不是一个好主意。 – Sorpigal 2012-02-02 12:28:36
如果将鼠标悬停在已分配给问题的标签上,则会发现,对于最糟糕的情况示例,“搜索替换”有3个关注者。我敢打赌,html有更多的追随者。当然,目标是让尽可能多的有见识的人看到你的问题。祝你好运! – shellter 2012-02-02 17:21:24