使用jq中的”or”运算符

huangapple go评论59阅读模式
英文:

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"

huangapple
  • 本文由 发表于 2023年3月4日 02:35:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定