【问题标题】:cURL awk { print } helpcURL awk { 打印 } 帮助
【发布时间】:2011-07-03 06:10:03
【问题描述】:

我在一个文件中有这个:

<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 } '

希望有人可以解释如何打印这两个词。谢谢!

【问题讨论】:

  • 请允许我欢迎您来到 StackOverflow,并提醒我们通常在这里做的三件事:1) 当您获得帮助时,请尝试在您的专业领域中回答问题 2)Read the FAQs 3)当你看到好的问答时,投票给他们using the gray triangles,因为系统的可信度是基于用户通过分享他们的知识而获得的声誉。还记得接受更好地解决您的问题的答案,如果有的话,by pressing the checkmark sign

标签: printing curl awk


【解决方案1】:
$ 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 解析器。

【讨论】:

    【解决方案2】:
    cat ~/Documents/weather.dat |awk 'BEGIN { FS = "[\"]" } ; /yweather:condition/ { print $2 } '
    

    替换所有的

    【讨论】:

      【解决方案3】:
      xmlstarlet sel -t -v "//@text" ~/Documents/weather.dat 2>/dev/null
      

      【讨论】:

      • 很好的答案。我以前不知道那个程序,我只是将它添加到我的工具箱中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 2011-07-18
      • 1970-01-01
      相关资源
      最近更新 更多