在同一行上打印多个值

时间:2022-03-08 02:11:59

I'm trying to parse a JSON document and print a couple of values on the same line. Is there a way to take the following document:

我正在尝试解析JSON文档并在同一行上打印几个值。有没有办法采取以下文件:

{
  "fmep": {
    "foo": 112,
    "bar": 234324,
    "cat": 21343423
  }
}

And spit out:

并吐出:

112 234324

I can get the values I want but they are printed on separate lines:

我可以得到我想要的值,但它们打印在不同的行上:

$ echo '{ "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } }' | jq '.fmep|.foo,.bar'

112  
234324

If there is an example somewhere that shows how to do this I would appreciate any pointers.

如果有一个示例显示如何执行此操作,我会很感激任何指针。

2 个解决方案

#1


8  

The easiest way in your example is to use String Interpolation along with the -r option. e.g.

您的示例中最简单的方法是使用字符串插值和-r选项。例如

echo '{ "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } }' | \
jq -r '.fmep| "\(.foo) \(.bar)"'

produces

112 234324

You may also want to consider putting the values in an array and using @tsv e.g.

您可能还需要考虑将值放在数组中并使用@tsv,例如

echo '{ "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } }' | \
jq -r '.fmep | [.foo, .bar] | @tsv'

which produces tab-separated

它产生制表符分隔

112 234324

#2


0  

Here is the syntax using joined output (-j):

以下是使用连接输出(-j)的语法:

jq -j '.fmep | .foo, " ", .bar, "\n"' payload.json

#1


8  

The easiest way in your example is to use String Interpolation along with the -r option. e.g.

您的示例中最简单的方法是使用字符串插值和-r选项。例如

echo '{ "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } }' | \
jq -r '.fmep| "\(.foo) \(.bar)"'

produces

112 234324

You may also want to consider putting the values in an array and using @tsv e.g.

您可能还需要考虑将值放在数组中并使用@tsv,例如

echo '{ "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } }' | \
jq -r '.fmep | [.foo, .bar] | @tsv'

which produces tab-separated

它产生制表符分隔

112 234324

#2


0  

Here is the syntax using joined output (-j):

以下是使用连接输出(-j)的语法:

jq -j '.fmep | .foo, " ", .bar, "\n"' payload.json