首页 > 解决方案 > Filtering JSON list in shell using jq

问题描述

Input :

{ "names" : ["name1","name2","name3","pat_name4"] }

Needed output :

{ "names" : ["name1","name2","name3"] }

Currently what i have by running command, to filter out all names starting with pat_

cat file | jq .names | grep -Ev '^(pat_)'

is this

["name1","name2","name3"]

Was wondering if there is some alteration to the jq command that could be done to get in the format needed.

标签: arraysbashshellfilterjq

解决方案


根据您的输入,调用:

$ jq -c '.names |= map(select(test("^pat_")|not))' 

产生:

{"names":["name1","name2","name3"]}

推荐阅读