从单行文本中抓取所有模式实例,编辑,输出到行分隔文本文件

时间:2012-11-24 06:40:31

标签: unix sed grep

我有一个文本块(单行),它是由标签和一堆其他垃圾分隔的URL列表。我想解析匹配' http。*"> RSS'的URL的整个块,编辑该模式的所有实例(以删除glob之后的所有内容),然后管道整个文件作为以行分隔的条目。

我认为我可以使用GREP执行此操作(然后使用SED编辑和添加新行),但GREP会抓取匹配的行,而不是匹配模式。我应该使用不同的命令吗?我也曾尝试使用SED在模式前面添加换行符(\ n),无论它出现在哪里,但也不起作用。

编辑:以下是我与之合作的数据示例:

OUT</a> (<a href="https://evilcakes.wordpress.com/rss">RSS</a>)</li><li><a href="http://eater.com/" title="">Eater National</a> (<a href="http://feeds.feedburner.com/EaterNational">RSS</a>)</li><li><a href="http://www.foodtechconnect.com" title="">Food+Tech Connect</a> (<a href="http://feeds.feedburner.com/foodtechconnect">RSS</a>)</li><li><a href="http://www.innatthecrossroads.com" title="">Inn at the Crossroads</a> (<a href="http://innatthecrossroads.com/feed/">RSS</a>)</li><li><a href="http://www.seriouseats.com/" title="">Serious Eats</a> (<a href="http://feeds.seriouseats.com/seriouseatsfeaturesvideos">RSS</a>)</li><li><a href="http://www.thatsnerdalicious.com" title="">That's Nerdalicious!</a> (<a href="http://www.thatsnerdalicious.com/feed/">RSS</a>)</li><li><a href="http://thedrunkenmoogle.com/" title="">The Drunken Moogle</a> (<a href="http://www.thedrunkenmoogle.com/rss">RSS</a>)</li></ul></li><li><h2 class="entry-title">Comics</h2><ul class="opmlGroup"><li><a

4 个答案:

答案 0 :(得分:3)

这是使用GNU grep的一种方式:

grep -oP 'http[^"]*(?=">RSS)' file

结果:

https://evilcakes.wordpress.com/rss
http://feeds.feedburner.com/EaterNational
http://feeds.feedburner.com/foodtechconnect
http://innatthecrossroads.com/feed/
http://feeds.seriouseats.com/seriouseatsfeaturesvideos
http://www.thatsnerdalicious.com/feed/
http://www.thedrunkenmoogle.com/rss

选项:

-o, --only-matching
    Print only the matched (non-empty) parts of a matching line, with each such 
    part on a separate output line.
-P, --perl-regexp
    Interpret PATTERN as a Perl regular expression. This is highly experimental
    and grep -P may warn of unimplemented features.

此外,您可能希望阅读lookaround assertions。 HTH。

<强> 编辑:

这是使用awk的另一种方式:

awk -F\" '{ for(i=1;i<NF;i++) if ($(i+1) ~ /RSS/) print $i }' file

结果:

https://evilcakes.wordpress.com/rss
http://feeds.feedburner.com/EaterNational
http://feeds.feedburner.com/foodtechconnect
http://innatthecrossroads.com/feed/
http://feeds.seriouseats.com/seriouseatsfeaturesvideos
http://www.thatsnerdalicious.com/feed/
http://www.thedrunkenmoogle.com/rss

答案 1 :(得分:3)

这可能适合你(GNU sed):

sed '/https\?:[^"]*/!d;s//\n&\n/;s/^[^\n]*\n//;P;D' file

答案 2 :(得分:1)

我把你的样本数据放在urls.dat。

cat urls.dat | awk '{n=split($0,a,"\""); for (i=1;i<=n;i++) if ( match( a[i], "^http" ) ) print a[i]; }'

答案 3 :(得分:1)

这是一种适用于GNU和BSD grep的方法:

<infile grep -Eo 'https?://[^"]+">RSS*' | grep -Eo '^[^"]+'

输出:

https://evilcakes.wordpress.com/rss
http://feeds.feedburner.com/EaterNational
http://feeds.feedburner.com/foodtechconnect
http://innatthecrossroads.com/feed/
http://feeds.seriouseats.com/seriouseatsfeaturesvideos
http://www.thatsnerdalicious.com/feed/
http://www.thedrunkenmoogle.com/rss
相关问题