我有一个问题在这里,但我是新的堆栈,并以某种方式线程被锁定或删除:Thread
我正在使用一个WordPress数据库,在“post_content”列中有60,000左右的“帖子”我要删除那些<p>
html标记以及它们之间的文本。 。我的帖子内容如下:
<p style="text-align: left;"><span style="color: #fffff;">
An entire paragraph of text around 200 words
</span></p>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="309" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
p
标签将是相同的,每个帖子只发生一次,例外情况是某些帖子可能有所不同的颜色。
预期输出应该是这样的:
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="309" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
我想删除段落标签中的所有文字。所以我想删除的是文本“200个单词的整个文本段落”这个文本在每个帖子上都不同,但是一个常量是<p>
打开和关闭标记。
从我上一个问题这个命令:用户“PS。”
awk '/<p/,/<\/p>/{next} {print $0}' inputfile
在我转储后,在.sql数据库上运行了。但在查看数据库后,文本仍然存在。
任何帮助都会非常感激。
更新:这个问题解决了:Ed Morton
Using GNU awk for multi-char RS this:
awk -v RS='</p>\\s*' -v ORS= '{sub(/<p.*/,"")} 1' file
答案 0 :(得分:2)
使用GNU awk进行多字符RS:
awk -v RS='</p>\\s*' -v ORS= '{sub(/<p.*/,"")} 1' file
无论文件中只有1对还是多对<p...</p>
,都会起作用,例如:
$ cat file
<p style="text-align: left;"><span style="color: #fffff;">
First entire paragraph of text around 200 words
</span></p>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="309" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
<p style="text-align: left;"><span style="color: #fffff;">
Second entire paragraph of text around 200 words
</span></p>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="309" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
$ awk -v RS='</p>\\s*' -v ORS= '{sub(/<p.*/,"")} 1' file
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="309" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="309" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
上述内容显然很脆弱,如果<p
可以出现在[Text_between_brackets]
中,则可能会失败。您可以在<p...
中指定的sub()
行越多,它就越脆弱,例如你可以/应该做更多这样的事情:
awk -v RS='</p>\\s*' -v ORS= '{sub(/<p style="text-align: left;"><span style="color: /,"")} 1' file
答案 1 :(得分:1)
您可以尝试以下sed命令 -
sed '/<p/,/<\/p/d' kk.txt
需要为</p
使用转义字符。
答案 2 :(得分:0)
我不会使用awk或sed或perl。正如您所发现的那样,正确管理正则表达式很困难。有一个老笑话:
有些人在面对问题时会思考 “我知道,我会使用正则表达式。”现在他们有两个问题。 - Jamie Zawinski,1997年
我甚至不会转储数据并编辑转储文件。这也很难。
更简单的解决方案是使用MySQL内置的XPath函数直接操作数据库中的每个帖子。我测试了以下解决方案,查询了您的示例帖子内容的一个版本,并删除了<p>
标记(以及其中的所有内容)。
mysql> SELECT post_content,
UpdateXml(post_content, '/p', '') AS post_content_without_p
FROM posts\G
显示内容前后的输出是:
*************************** 1. row ***************************
post_content: <p style="text-align: left;"><span style="color: #fffff;">
An entire paragraph of text around 200 words
</span></p>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="309" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
post_content_without_p:
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="309" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
[Text_between_brackets]
<iframe src="http://somewebsite.com" width="250" height="250" frameborder="0" marginwidth="0" marginheight="0" scrolling="NO"></iframe>
1 row in set (0.00 sec)
UpdateXml()函数是MySQL记录的内置函数的一部分。它需要三个参数:
如果您对查询的效果感到满意,则可以更新表中的内容而无需转储和恢复:
mysql> UPDATE posts SET post_content = UpdateXml(post_content, '/p', '');
在尝试这样的更改之前,请务必进行备份!或者在实验时将数据复制到另一个数据库。