英文:
Use or operator in jq
问题
我正在使用jq来解析JSON。JSON中有一个名为"players[]"的数组,其中包含一个名为"name"的键,其示例值如下:
ZZZ-104TH-082-ADV-004
ZZZ-149TH-435-ADV-WL-006
GDS-123-CIC-007_temp-blocked
LRMR-121-CIC-002_temp-removed
NOS-343-CIC-003_Failed
E180TH-426-CIC-008
LBRTY-185-CIC-005
我有以下jq命令:
jq '.players[] | if .name | contains( "ZZZ" ) then empty else .name end'
这将返回以下名称:
GDS-123-CIC-007_temp-blocked
LRMR-121-CIC-002_temp-removed
NOS-343-CIC-003_Failed
E180TH-426-CIC-008
LBRTY-185-CIC-005
我还想要排除包含"Failed"或"temp"的名称,这将返回以下结果:
E180TH-426-CIC-008
LBRTY-185-CIC-005
我尝试了以下命令:
jq '.players[] | if .name | contains( "ZZZ" ) or contains( "Failed" ) then empty else .name end'
但这只排除了包含"ZZZ"的名称。如何筛选掉包含"ZZZ"、"Failed"或"temp"的名称?
英文:
I'm using jq to parse JSON. The JSON has an array called "players[]" which contains a key "name" with the following sample values:
ZZZ-104TH-082-ADV-004
ZZZ-149TH-435-ADV-WL-006
GDS-123-CIC-007_temp-blocked
LRMR-121-CIC-002_temp-removed
NOS-343-CIC-003_Failed
E180TH-426-CIC-008
LBRTY-185-CIC-005
I have this jq line:
jq '.players[] | if .name | contains( "ZZZ" ) then empty else .name end'
Which returns the following names:
GDS-123-CIC-007_temp-blocked
LRMR-121-CIC-002_temp-removed
NOS-343-CIC-003_Failed
E180TH-426-CIC-008
LBRTY-185-CIC-005
I would like to also exclude names that contain "Failed" or "temp" which would return the following:
E180TH-426-CIC-008
LBRTY-185-CIC-005
I tried
jq '.players[] | if .name | contains( "ZZZ" ) or contains( "Failed" ) then empty else .name end'
But that only excluded the names containing "ZZZ". how can I filter out names that contain "ZZZ" or "Failed" or "temp"?
答案1
得分: 1
我会选择使用 select
(如果其参数求值为 false
,即没有匹配项,它会发出 empty
),以及正则表达式的 test
:
jq '.players[].name | select(test("ZZZ|temp|Failed") | not)'
"E180TH-426-CIC-008"
"LBRTY-185-CIC-005"
英文:
I'd go with select
(which emits empty
if its argument evaluates to false
, i.e. there is no match), and test
for regular expressions:
jq '.players[].name | select(test("ZZZ|temp|Failed") | not) '
"E180TH-426-CIC-008"
"LBRTY-185-CIC-005"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论