分析 curl 结果时遇到的问题
请求一个curl结果
var=`curl -l "http://www.kuaidi100.com/query?type=yuantong&postid=11111111111"`
意图是想对返回的json数据进行如下处理:
匹配出结果中的几对key-value
#错误示例
$val | grep -o -P '\"abstract\":.*?(?=,)'
结果是提示文件名过长,且可能存在的空格会导致commond not found
显然这里我是使用错误了
将请求的结果保存在文件中再读取:
cat file | grep -o -P '\"abstract\":.*?(?=,)'
可以取到结果,但事实上整个流程是在循环中进行的
sudo rm -rf data/res.log
while read query
do
var=$(command)
echo 'kw:'$query',var:'$var >> data/res.log;
done < querys.log
还是希望能直接在循环中直接处理完结果
尝试把grep
的操作拼接在var
中即:
var=`curl -l "http://www.kuaidi100.com/query?type=yuantong&postid=11111111111" | grep -o -P '\"context\":.*?(?=,)'`
#之前随便找的一个api接口,没想到失效了,这里备一份
{
"message": "ok",
"nu": "11111111111",
"ischeck": "1",
"com": "yuantong",
"status": "200",
"condition": "F00",
"state": "3",
"data": [
{
"time": "2020-01-09 09:43:08",
"context": "查无结果",
"ftime": "2020-01-09 09:43:08"
}
]
}
确实可以取到想要的结果。
但是,如果想要取多对数据应该怎么处理?
望不吝赐教,感谢!
推荐文章: