shell数组的差集

发布时间 2023-06-12 19:59:19作者: lvmxh

https://stackoverflow.com/questions/29396154/jq-setdiff-of-two-arrays

1.

echo -n '{"all":["A","B","C","ABC"],"some":["B","C"]}' | jq '. as $d | .all | del(.[ indices($d.some[])[] ])'

2.

array1='["A","B","C","ABC"]'
array2='["B","C"]'

jq -n --argjson array1 "$array1" --argjson array2 "$array2" '$array1-$array2'

jq -n --argjson array1 "$array1" --argjson array2 "$array2" 
'{"all": $array1,"some":$array2} | .all-.some'

3.

echo -n '{"all":["A","B","C","ABC"],"some":["B","C"]}' | jq '  . as $d
| .all
| [indices($d.some[])[]] as $found
| del(.[ $found[] ])
| "all", $d.all, "some", $d.some, "removing indices", $found, "result", .‘