2012-02-02 177 views
0

您好我有以下文件搜索和替换

 <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标签,如 <a href=""> </a>其中没有wmode=transparent。 上面发布的文件非常大,大约有30K行在HTML中,所以我只发布了相关行。 我在Ubuntu系统上。

+0

有没有简单的方法来做到这一点与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

+0

如果将鼠标悬停在已分配给问题的标签上,则会发现,对于最糟糕的情况示例,“搜索替换”有3个关注者。我敢打赌,html有更多的追随者。当然,目标是让尽可能多的有见识的人看到你的问题。祝你好运! – shellter 2012-02-02 17:21:24

回答

0

这里是一个link在哪里你可以找到你的问题的答案。

你的情况

,你必须创建一个脚本文件sed的像

S /的wmode =透明// g^
S /宽= 320 // g^

和运行的东西这样的:

SED -f replace_file in.txt> out.txt

我希望它对你有帮助。

有一个愉快的一天

+0

这并不能解决问题。他希望识别wmode = transparent或width = 320的标签,然后删除*整个标签*,而不仅仅是这些部分。由于不能保证每个标签都在自己的行上,所以'sed'特别不合适。 – Sorpigal 2012-02-02 12:30:15

1

由于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可以为此做好。