繁体   English   中英

我想使用awk从压缩的XML提要中解析文本

[英]I want to parse text from a compressed XML feed using awk

我试图从http://rss.slashdot.org/Slashdot/slashdot的压缩XML提要中解析<title><description> 我正在尝试执行以下操作

curl --silent "http://rss.slashdot.org/Slashdot/slashdot" | awk '/\btitle\b(.*?)\bdescription\b/' 

grep -E等,但是我无法获得想要的子字符串。 它总是在压缩后返回整个XML,并且数据在一行中。

我可以通过在文本编辑器中运行它来测试我的Regex字符串。

感谢您的帮助!! 谢谢!

使用XML解析器会有所帮助,这里使用perlXML::Twig进行测试。 使其适应您的需求。

script.pl内容:

#!/usr/bin/env perl

use warnings;
use strict;
use XML::Twig;

my $twig = XML::Twig->new(
    twig_handlers => {
        'title' => \&extract_text,
        'description' => \&extract_text,
    },  
)->parsefile( shift );

sub extract_text {
    my ($t, $e) = @_; 
    printf qq|%s\n=================\n|, $e->tag;
    printf qq|%s\n\n|, $e->text;
}

像这样运行:

curl --silent "http://rss.slashdot.org/Slashdot/slashdot" | perl script.pl -

对于每对标题和描述,这类似于以下内容:

title
=================
Proof-of-Concept Port of XBMC to SDL 2.0 and Wayland

description
=================
hypnosec wrote in with news that XBMC has  ...

这是XSLT解决方案:

curl -s -o- http://rss.slashdot.org/Slashdot/slashdot | xsltproc slashdot.xsl -

slashdot.xsl在哪里

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />

<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>

<xsl:template match="/">
    <xsl:apply-templates select='//item' />
</xsl:template>

<xsl:template match='//item'>
    <xsl:value-of select='title' /><xsl:value-of select='$newline' />
    <xsl:text>====</xsl:text><xsl:value-of select='$newline' />
    <xsl:value-of select='description' /><xsl:value-of select='$newline' />
    <xsl:value-of select='$newline' />
</xsl:template>

</xsl:stylesheet>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM