jq print character inside output

前端 未结 3 1738
余生分开走
余生分开走 2021-01-22 12:11

I want print \"/\" separator inside output title.

curl  -s http://cd0a4a.ethosdistro.com/?json=yes \\
    | jq -c \'.rigs|.\"0d6b27\",.\"50dc35\"|[.         


        
相关标签:
3条回答
  • 2021-01-22 12:44

    You're doing a lot of unnecessary calls just to process the data. Your commands could be drastically simplified.

    • You don't need to explicitly key into the .rigs object to get their values, you could just access them using [].
    • You don't need the sed call to strip the quotes, just use the raw output -r.
    • You don't need the awk call to add the header, you could just output an additional row from jq.

    So your command turns into this instead:

    $ curl -s http://cd0a4a.ethosdistro.com/?json=yes \
    | jq -r '["version", "GPU_driver", "miner", "gpu"],
             (.rigs[] | [.version, .driver, .miner, "\(.gpus)/\(.miner_instance)"])
                 | @csv' \
    | csvlook -I
    
    0 讨论(0)
  • 2021-01-22 12:46

    Here are some suggestions for simplification:

    • use the --raw-output option to jq to remove extraneous back-slashes
    • there is no need to remove the quotes, csvlook does it for you
    • no need for awk to add a title line, use a sub-shell
    • no need to specify rigs implicitly, use .[]

    Here is an example:

    (
      echo version,GPU_driver,miner,gpu
      curl -s 'http://cd0a4a.ethosdistro.com/?json=yes' |
      jq -r '
        .rigs | .[] |
        [ .version, .driver , .miner  , "\(.gpus)/\(.miner_instance)" ] |
        @csv
      '
    ) |
    csvlook
    

    Output:

    |----------+------------+----------+------|
    |  version | GPU_driver | miner    | gpu  |
    |----------+------------+----------+------|
    |  1.2.3   | nvidia     | ethminer | 2/2  |
    |  1.2.4   | amdgpu     | ethminer | 1/1  |
    |----------+------------+----------+------|
    
    0 讨论(0)
  • 2021-01-22 13:02

    Since you already use string interpolation for that specific field, simply include the character you need (slash /) inside the string, like this:

    curl ... | jq -c '... [.version,.driver,.miner,"\(.gpus)/\(.miner_instance)"] ...'
    

    In your case (the complete line):

    curl -s http://cd0a4a.ethosdistro.com/?json=yes | jq -c '.rigs|."0d6b27",."50dc35"|[.version,.driver,.miner,"\(.gpus)/\(.miner_instance)"]|@csv' | sed 's/\\//g;s/\"//g' | gawk 'BEGIN{print  "version" "," "GPU_driver" "," "miner" "," "gpu"} {print $0}' | csvlook -I
    
    0 讨论(0)
提交回复
热议问题