2014-12-07 75 views
-2
jsonval() { 
temp=`echo $haystack | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '  {n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed ' s/\"//g' | grep -w $needle` 
    echo ${temp##*|} 
} 

dev_key='xxxxxxxxxxxx' 
zip_code='48446' 
city='Lapeer' 
state='MI' 
red=$(tput setaf 1) 
textreset=$(tput sgr0) 

haystack=$(curl -Ls -X GET    http://api.wunderground.com/api/$dev_key/conditions/q/$state/$city.json) 
needle='temperature_string' 

temperature=$(jsonval $needle $haystack) 

needle='weather' 
current_condition=$(jsonval $needle $haystack) 

echo -e '\n' $red $current_condition 'and' $temperature $textreset '\n' 

此代码应该使用开发人员密钥来解析json天气数据到终端以调用信息。有人可以解释这段代码在做什么吗? sed在做什么特殊字符?

这是完整的代码,有人可以解释sed在做什么,我知道它应该作为一种替代方法,但为什么会出现如此多的斜杠和特殊字符?

什么是echo $ {temp ## * |}在做什么,所有这些特殊字符都让我很难理解这段代码。

+1

运行这种CMD的用你自己的输入并看到...... echo“a \\ b”| sed's/\\\\\ // \ // g' http://www.grymoire.com/Unix/Sed.html#uh-1 – 2014-12-07 04:28:27

+1

其次,它的风格并不好,写得一团糟,从中学习会引导你错误的方式。 – BMW 2014-12-07 04:34:04

+0

“$干草堆”的价值是什么?如果没有这个值,就不可能知道sed对字符串做了什么,你将不得不将代码切成片段,尝试每个片断查看它们的输出。 – repzero 2014-12-07 04:53:40

回答

1

看起来这个命令试图解析这是一个好主意,因为工具箱中有一些不错的东西。其中之一是。它擅长格式化JSON输出或检索复杂数据源中的项目。例如:

file.json

{ 
    "items": [ 
     { 
      "tags": [ 
       "bash", 
       "vim", 
       "zsh" 
      ], 
      "owner": { 
       "reputation": 178, 
       "user_id": 22734, 
       "user_type": "registered", 
       "profile_image": "https://www.gravatar.com/avatar/25ee9a1b9f5a16feb1432882a9ef2f06?s=128&d=identicon&r=PG", 
       "display_name": "Brad Parks", 
       "link": "http://unix.stackexchange.com/users/22734/brad-parks" 
      }, 
      "is_answered": false, 
      "view_count": 2, 
      "answer_count": 0, 
      "score": 0, 
      "last_activity_date": 1417919326, 
      "creation_date": 1417919326, 
      "question_id": 171907, 
      "link": "http://unix.stackexchange.com/questions/171907/use-netrw-or-nerdtree-in-zsh-bash-to-select-a-file-by-browsing", 
      "title": "Use Netrw or Nerdtree in Zsh/Bash to select a file BY BROWSING?" 
     } 
    ] 
} 

输出搜索所有者的子HASHoutput

不严重推倒重来;)

相关问题