使用XML解析器將有所幫助,在這裏使用perl
和XML::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 -
這yiedls像每對標題和描述如下:
title
=================
Proof-of-Concept Port of XBMC to SDL 2.0 and Wayland
description
=================
hypnosec wrote in with news that XBMC has ...
「我想分析[...] XML [...]用awk」 - 這就是你搞砸了。 – 2013-03-09 21:51:13
萬一你必須用awk來做,'但是我不能得到我想要的子串',你想要什麼? – Kent 2013-03-09 22:03:29