2011-02-24 84 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