V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
krisbai
V2EX  ›  Linux

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

  •  
  •   krisbai · 2017-08-22 10:29:50 +08:00 · 4099 次点击
    这是一个创建于 2651 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如题 :想把 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
    15 条回复    2017-08-23 14:26:01 +08:00
    Chyroc
        1
    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
        2
    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
        3
    zhs227  
       2017-08-22 10:49:02 +08:00   ❤️ 1
    yum install jq
    https://stedolan.github.io/jq/manual/

    jq 比较好用。下面是一个例子:
    https://jqplay.org/s/vsOpssDUO_
    krisbai
        4
    krisbai  
    OP
       2017-08-22 10:58:45 +08:00
    @zhs227 嗯,这个工具确实蛮好用的,之前也在用,想请教下 我格式化出来的 带逗号,这个如何在获取参数的时候取消掉?
    比如 :cat page.txt |jq .|grep request_4xx| cut -d ":" -f 2 得到的值 :1505, 想取消掉逗号
    zhs227
        5
    zhs227  
       2017-08-22 11:00:27 +08:00
    试试 `jq --raw-output`
    zhs227
        6
    zhs227  
       2017-08-22 11:02:11 +08:00
    用 jq 直接取字段,不要用 grep 处理。1 楼的答案。 比如 jq ".data.request_4xx"
    NoAnyLove
        7
    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
        8
    krisbai  
    OP
       2017-08-22 11:20:04 +08:00
    @Chyroc 感谢
    krisbai
        9
    krisbai  
    OP
       2017-08-22 11:20:26 +08:00
    @zhs227 哈哈,不知道我格式有问题还是 ,得到的数据有点问题
    krisbai
        10
    krisbai  
    OP
       2017-08-22 11:20:40 +08:00
    @NoAnyLove 谢谢大牛,这个办法真直接!
    krisbai
        11
    krisbai  
    OP
       2017-08-22 13:16:32 +08:00
    @NoAnyLove 再请教下,生成的数据都在一行。如果要达到换行的效果,\n 应该加在那里呢?
    ms2008
        12
    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
        13
    UnknownR  
       2017-08-22 20:32:04 +08:00 via iPhone
    输出成 json 或者 xml 就可以了,都是数据存储格式,会自动调整的
    NoAnyLove
        14
    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
        15
    krisbai  
    OP
       2017-08-23 14:26:01 +08:00
    @NoAnyLove 好,谢谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2891 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 09:03 · PVG 17:03 · LAX 01:03 · JFK 04:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.