虽然接受的答案有效,但我觉得这更具可读性
$
$ echo '[{"Address": "The Sq", "n": 1}, {"Address": "1 Bridge Rd", "n": 2}]' | \
jq '.[] | .Address | select(.|test("^[0-9]"))'
"1 Bridge Rd"
$
$ echo '[{"Address": "The Sq", "n": 1}, {"Address": "1 Bridge Rd", "n": 2}]' | \
jq '.[] | select(.Address|test("^[0-9]"))'
{
"Address": "1 Bridge Rd",
"n": 2
}
$
如果你想做一些不同的过滤:
$ echo '[{"name": "john doe", "sex": "male", "age": 26, "occupation": "city planner", "cod": "asphyxiation"}, {"name": "jane doe", "sex": "male", "age": 24, "occupation": "beautician", "cod": "strangulation"}, {"name": "crispy lips", "sex": "male", "age": 38, "occupation": "convicted killer"} ]' > in.json
$ cat in.json | jq .
[
{
"name": "john doe",
"sex": "male",
"age": 26,
"occupation": "city planner",
"cod": "asphyxiation"
},
{
"name": "jane doe",
"sex": "male",
"age": 24,
"occupation": "beautician",
"cod": "strangulation"
},
{
"name": "crispy lips",
"sex": "male",
"age": 38,
"occupation": "convicted killer"
}
]
$
然后我们可以像这样进行基本的正则表达式过滤/转换:
$ cat in.json | jq '.[] | .name'
"john doe"
"jane doe"
"crispy lips"
$
$ cat in.json | jq '.[] | .name | select(.|test(".*doe"))'
"john doe"
"jane doe"
$
$ cat in.json | jq '.[] | select(.name|test(".*doe"))'
{
"name": "john doe",
"sex": "male",
"age": 26,
"occupation": "city planner",
"cod": "asphyxiation"
}
{
"name": "jane doe",
"sex": "male",
"age": 24,
"occupation": "beautician",
"cod": "strangulation"
}
$
$ cat in.json | jq '.[] | select(.name|test(".*doe")) | {n: .name, a: .age}'
{
"n": "john doe",
"a": 26
}
{
"n": "jane doe",
"a": 24
}
$
$