如何使用jq漂亮的打印,让多个值在同一行? [英] How to pretty print using jq, so that multiple values are on the same line?

查看:229
本文介绍了如何使用jq漂亮的打印,让多个值在同一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
frameGrid:{
size :[
24,
24
],
维度:[
1,
1
],
姓名:[
[
default
]
]
}
}
pre>

我想看到的更类似于:

pre $ {
frameGrid:{
size:[24,24],
dimensions:[1,1],
names:[ default]]
}
}

我知道这两种形式都是有效的,而jq有一个紧凑/漂亮的打印模式。但是中间有什么东西吗?我正在寻找格式化一个更大的json文件,有更多的数组值比这个,所以它很容易阅读和打印。也许我只是使用错误的工具来完成这项工作?



(请原谅可怕的格式选择,似乎code-sample不太喜欢json格式)虽然这可能是最好的使用像这样的工具 解决方案

如果你的json不是太复杂,你可以使用第二个jq调用来后处理第一个的输出。例如,如果您的数据在 data.json

  $ jq -M。 data.json | jq -MRsr'gsub(\\\
+;)| gsub(\\\
];])'

产生

  {
frameGrid:{
size:[24,24],
dimensions:[1,1],
names:[[default]]
}
}


What I get from jq is:

{
  "frameGrid": {
    "size": [
      24,
      24
    ],
    "dimensions": [
      1,
      1
    ],
    "names": [
      [
        "default"
      ]
    ]
  }
}

What I would like to see is something more like this:

{
  "frameGrid": {
    "size": [24,24], 
    "dimensions": [1,1],
    "names": [["default"]]
  }
}

I know both forms are valid, and that jq has a compact/pretty print mode. But is there something in-between? I'm looking to somehow format a larger json file that has many more array values than this, so that it's easily readable and printable. Maybe I'm just using the wrong tool for this job?

(please excuse the horrid formating choice. Seems code-sample doesn't like json formats much)

解决方案

While it is probably best to use a tool like the one peak suggested if your json isn't too complex you could use a second jq invocation to postprocess the output of the first. For example if your data is in data.json

$ jq -M . data.json | jq -MRsr 'gsub("\n      +";"")|gsub("\n    ]";"]")'

produces

{
  "frameGrid": {
    "size": [24,24],
    "dimensions": [1,1],
    "names": [["default"]]
  }
}

这篇关于如何使用jq漂亮的打印,让多个值在同一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆