本文介绍了在同一行上打印多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解析JSON文档,并在同一行上打印几个值.有没有办法获取以下文件:
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:
{
"fmep": {
"foo": 112,
"bar": 234324,
"cat": 21343423
}
}
然后吐出来:
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.
推荐答案
示例中最简单的方法是使用字符串插值以及-r
选项.例如
The easiest way in your example is to use String Interpolation along with the -r
option. e.g.
echo '{ "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } }' | \
jq -r '.fmep| "\(.foo) \(.bar)"'
产生
112 234324
您可能还需要考虑将值放入数组中,并使用 @tsv 例如
You may also want to consider putting the values in an array and using @tsv e.g.
echo '{ "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } }' | \
jq -r '.fmep | [.foo, .bar] | @tsv'
产生制表符分隔的
112 234324
这篇关于在同一行上打印多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!