请教下如何提取 curl 格式化输出的值

2017-08-22 10:29:50 +08:00
 krisbai
如题 :想把 curl 得到的值按照自己想要的格式输出到文本,方便监控
curl 输出的值:
{"success":true,"data":{"request_5xx":1,"traffic_write":10866877,"average_traffix_write":426.43633010242,"average_traffic_read":429.33143664404,"traffic_read":10940653,"average_request_time":0.44471063102306,"total_count":25483,"request_3xx":0,"request_2xx":23981,"request_4xx":1501,"total_request_time":11332.561010361}
想得到的格式:
traffic_write:10866877
request_4xx:1501
4100 次点击
所在节点    Linux
15 条回复
Chyroc
2017-08-22 10:41:39 +08:00
echo '{"success":true,"data":{"request_5xx":1,"traffic_write":10866877,"average_traffix_write":426.43633010242,"average_traffic_read":429.33143664404,"traffic_read":10940653,"average_request_time":0.44471063102306,"total_count":25483,"request_3xx":0,"request_2xx":23981,"request_4xx":1501,"total_request_time":11332.561010361}}' | jq '. | {traffic_write: .data.traffic_write, request_4xx: .data.request_4xx}'
Chyroc
2017-08-22 10:42:06 +08:00
把 echo '{"success":true,"data":{"request_5xx":1,"traffic_write":10866877,"average_traffix_write":426.43633010242,"average_traffic_read":429.33143664404,"traffic_read":10940653,"average_request_time":0.44471063102306,"total_count":25483,"request_3xx":0,"request_2xx":23981,"request_4xx":1501,"total_request_time":11332.561010361}}' 换成 curl xxxx 即可
zhs227
2017-08-22 10:49:02 +08:00
yum install jq
https://stedolan.github.io/jq/manual/

jq 比较好用。下面是一个例子:
https://jqplay.org/s/vsOpssDUO_
krisbai
2017-08-22 10:58:45 +08:00
@zhs227 嗯,这个工具确实蛮好用的,之前也在用,想请教下 我格式化出来的 带逗号,这个如何在获取参数的时候取消掉?
比如 :cat page.txt |jq .|grep request_4xx| cut -d ":" -f 2 得到的值 :1505, 想取消掉逗号
zhs227
2017-08-22 11:00:27 +08:00
试试 `jq --raw-output`
zhs227
2017-08-22 11:02:11 +08:00
用 jq 直接取字段,不要用 grep 处理。1 楼的答案。 比如 jq ".data.request_4xx"
NoAnyLove
2017-08-22 11:12:33 +08:00
话说直接用命令行下的 tr 和 grep 就行了啊,不需要安装额外的工具:

echo '{"request_5xx":1,"traffic_write":10866877,"average_traffix_write":426.43633010242,"average_traffic_read":429.33143664404,"traffic_read":10940653,"average_request_time":0.44471063102306,"total_count":25483,"request_3xx":0,"request_2xx":23981,"request_4xx":1501,"total_request_time":11332.561010361}' | tr -d '"' | grep -oP '(traffic_write:\d+)|(request_4xx:\d+)'
krisbai
2017-08-22 11:20:04 +08:00
@Chyroc 感谢
krisbai
2017-08-22 11:20:26 +08:00
@zhs227 哈哈,不知道我格式有问题还是 ,得到的数据有点问题
krisbai
2017-08-22 11:20:40 +08:00
@NoAnyLove 谢谢大牛,这个办法真直接!
krisbai
2017-08-22 13:16:32 +08:00
@NoAnyLove 再请教下,生成的数据都在一行。如果要达到换行的效果,\n 应该加在那里呢?
ms2008
2017-08-22 15:29:26 +08:00
```
echo '{"success":true,"data":{"request_5xx":1,"traffic_write":10866877,"average_traffix_write":426.43633010242,"average_traffic_read":429.33143664404,"traffic_read":10940653,"average_request_time":0.44471063102306,"total_count":25483,"request_3xx":0,"request_2xx":23981,"request_4xx":1501,"total_request_time":11332.561010361}}' | python -mjson.tool
```
UnknownR
2017-08-22 20:32:04 +08:00
输出成 json 或者 xml 就可以了,都是数据存储格式,会自动调整的
NoAnyLove
2017-08-23 13:18:40 +08:00
@krisbai 显示成一行了?我这里显示的是两行啊,你是要把两行变成一行?还是一行变成两行?反正这两种都可以通过 sed 实现。

两行内容变成一行:
echo 'traffic_write:10866877\nrequest_4xx:1501' | sed 'N;s/\n/\t/g'

一行内容变成两行:
echo 'traffic_write:10866877\trequest_4xx:1501' | sed 's/\s\+/\n/g'

把对应的 sed 命令继续用管道接在之前的命令后面就行了。
krisbai
2017-08-23 14:26:01 +08:00
@NoAnyLove 好,谢谢!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/384766

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX