2011-02-24 85 views
0

我有这样的一个文件中:卷曲AWK {}打印帮助

<yweather:condition text="Partly Cloudy" code="29" temp="56" date="Wed, 23 Feb 2011 6:53 pm MST" /> 

我使用此代码,试图打印“晴间多云”,虽然只有“晴”是没有得到打印。

grep "yweather:condition" ~/Documents/weather.dat | awk '{ print $2 }' | awk 'BEGIN { FS = "[\"]" } ; { print $2 } ' 

希望有人能解释如何让这两个单词打印。谢谢!

+0

请允许我欢迎你们来的StackOverflow,并提醒三件事,我们通常在这里做的:1)当你得到帮助,尽量给它太**回答问题**在您的专业领域2)['阅读常见问题](http://tinyurl.com/2vycnvr)3)当您看到好的问答时,将它们投票['使用灰色三角形](http:// i .imgur.com/kygEP.png),因为系统的可信度基于用户通过分享知识获得的声誉。还请记住接受更好地解决您的问题的答案,如果有的话,['通过按复选标记符号](http://i.imgur.com/uqJeW.png) – 2011-02-24 04:10:31

回答

0
cat ~/Documents/weather.dat |awk 'BEGIN { FS = "[\"]" } ; /yweather:condition/ { print $2 } ' 

替换所有

+0

现货!那就是诀窍。非常感谢;-) – Larry 2011-02-24 03:25:49

0
$ echo "<yweather:condition text="Partly Cloudy" code="29" temp="56" date="Wed, 23 Feb 2011 6:53 pm MST" />" | awk '/weather/{gsub(/.*text=|code=.*/,"")}1' 
Partly Cloudy 

$ echo "<yweather:condition text="Partly Cloudy" code="29" temp="56" date="Wed, 23 Feb 2011 6:53 pm MST" />" | ruby -e 'puts gets.scan(/text=(.*)code=/)' 
Partly Cloudy 

如果您有更复杂的情况,请使用真正的XML/HTML解析器。

+0

感谢您的帮助,它似乎没有为我工作。 – Larry 2011-02-24 04:16:56

3
xmlstarlet sel -t -v "//@text" ~/Documents/weather.dat 2>/dev/null 
+0

很好的答案。我以前不知道这个程序,我只是将它添加到我的工具箱中。 – 2011-02-26 23:02:57