Chinaunix

标题: 文本处理求助-删除html标签 [打印本页]

作者: bikkuri    时间: 2016-06-21 10:36
标题: 文本处理求助-删除html标签
大家好!
我有一个问题向大家请教。
有以下一个文本,我想把所有的html标签去掉,即把用尖括号<>括起来的内容全部去掉,另外空行也去掉。
原始文本:
<head><meta http-equiv='refresh' content='3600'>
<title>Welcome Page</title>
<base target=_blank></head>
<body>
<img src=../test.png><br><br><table border='2' align='center'>

If you have any question or comment, please send an e-mail to the <a href="mailto:master@abc.com">webmaster</a>.</pre></tt><br></td></tr></table><br>
<center><img src=/cgi-bin/dream/dream.cgi?id=test></center></body>
</html>
期望得到的文本:
Welcome Page
If you have any question or comment, please send an e-mail to the webmaster.
谢谢大家。

作者: moperyblue    时间: 2016-06-21 10:36
本帖最后由 moperyblue 于 2016-06-21 11:18 编辑
  1. sed 's/<[^>]*>//g;/^\s*$/d'
  2. awk -vRS='<[^>]*>' -vORS="" 'NF{print $0}'|sed '/^\s*$/d'
复制代码

作者: 过过招    时间: 2016-06-21 10:47
这玩意儿吧,看的头大。

拿来主义,谷歌  html2txt,分分钟搞定咯……
作者: jason680    时间: 2016-06-21 10:51
$ awk '{gsub("<[^>]+>","")}!sub("^[\t ]*$","")' FILE
Welcome Page
If you have any question or comment, please send an e-mail to the webmaster.


作者: Herowinter    时间: 2016-06-21 10:52
回复 1# bikkuri

  1. sed 's/<[^>]*>//g;/^$/d' i
  2. Welcome Page
  3. If you have any question or comment, please send an e-mail to the webmaster.
复制代码

作者: wjemail    时间: 2016-06-21 11:09
  1. # sed -e 's/<[^>]*>//g' -e '/^$/d' q14
  2. Welcome Page
  3. If you have any question or comment, please send an e-mail to the webmaster.
复制代码

作者: liion631818    时间: 2016-06-21 12:19
回复 1# bikkuri
  1. sed 's/<[^>]*>//g' | sed -n '/^$/!p'
复制代码

作者: ll104567    时间: 2016-06-21 13:41
  1. sed -e 's/<\/.*>//g' -e 's/<.*>//g' -e '/^$/d'
复制代码
贺哥?
作者: LikeLx    时间: 2016-06-21 13:59
  1. sed '/</{N;s/<[^\>]*>//g}'
复制代码

作者: bikkuri    时间: 2016-06-21 14:08
谢谢大家的帮助!
作者: sunzhiguolu    时间: 2016-06-21 15:26
  1. perl -nle '@a=/(?:\A|(?<=>))[^<>]+(?=<)/g;print @a if(@a)' f
复制代码
Welcome Page
If you have any question or comment, please send an e-mail to the webmaster.

作者: 251744647    时间: 2016-06-21 16:29
sed 's/<[^>]*>//g;/^$/d' FILE
作者: sunzhiguolu    时间: 2016-10-03 21:19
  1. perl -nle 's{</?[^>]+>}{}g;print if(length)' f
复制代码

作者: moperyblue    时间: 2016-10-04 17:26

  1. awk '{gsub(/<[^>]*>/,"")}NF'
复制代码

作者: jcdiy0601    时间: 2016-10-08 10:51
  1. sed 's/<[^<]*>//g;/^$/d' file
  2. Welcome Page
  3. If you have any question or comment, please send an e-mail to the webmaster.
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2