使用jq在shell中过滤JSON列表 [英] Filtering JSON list in shell using jq
本文介绍了使用jq在shell中过滤JSON列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
输入:
{ "names" : ["name1","name2","name3","pat_name4"] }
所需的输出:
{ "names" : ["name1","name2","name3"] }
当前我通过运行命令所拥有的内容,以过滤出以 pat _
Currently what i have by running command, to filter out all names starting with pat_
cat file | jq .names | grep -Ev '^(pat_)'
这是
["name1","name2","name3"]
想知道是否可以对 jq 命令进行一些更改以获取所需的格式.
Was wondering if there is some alteration to the jq command that could be done to get in the format needed.
推荐答案
根据您的输入,调用:
$ jq -c '.names |= map(select(test("^pat_")|not))'
产生:
{"names":["name1","name2","name3"]}
这篇关于使用jq在shell中过滤JSON列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文