我有一个OPML文件,我想要解析链接和名称,以便创建一个HTML格式的列表。
<outline text="Wired Features" type="rss" xmlUrl="http://downloads.wired.com/podcasts/xml/features.xml?_kip_ipx=1854665749-1310493405" htmlUrl="http://www.wired.com" />
<outline text="ArcSight Podcasts" type="rss" xmlUrl="http://www.arcsight.com/podcasts/itunes/" htmlUrl="http://www.arcsight.com" />使用SED或类似的东西,我希望打印相应的HTML输出中的项目,即
<a href="http://downloads.wired.com/podcasts/xml/features.xml?_kip_ipx=1854665749-1310493405" title="http://www.wired.com">Wired Features</a>发布于 2011-11-05 05:34:10
perl -nle'
($text) = /text="(.*?)"/ ;
($url) = /xmlUrl=(".*?")/ ;
($title) = /htmlUrl=(".*?")/;
/./ and printf "<a href=%s title=%s>%s</a>\n",
$url, $title, $text;
' infile假设在感兴趣的部分中没有嵌入换行符。
使用XMLgawk
xgawk -lxml 'XMLSTARTELEM {
printf "<a href=%s title=>%s>%s</a>\n",
q XMLATTR["xmlUrl"] q, q XMLATTR["htmlUrl"] q, XMLATTR["text"]
}' q=\" infile编辑: Perl解决方案可以用一个正则表达式重写:
perl -nle'
/text="(.*?)".*xmlUrl=(".*?").*htmlUrl=(".*?")/
and printf "<a href=%s title=%s>%s</a>\n",
$2, $3, $1;
' infile 发布于 2011-11-05 06:57:43
这个sed解决方案可能会起作用:
sed 's/^<outline text="\([^"]*\)" type="rss" xmlUrl=\("[^"]*"\) htmlUrl=\("[^"]*"\) \/>/<a href=\2 title=\3>\1<\/a>/' input_filehttps://stackoverflow.com/questions/8015858
复制相似问题