比较unix中的数组元素和打印不匹配

时间:2015-05-14 10:23:06

标签: bash

我想比较下面的数组,只打印/存储另一个数组中的不同项。你可以帮忙。

replace

如果我在bash中尝试这个,它只打印数组1中的值

eg Array1  for :
20150313 20150324 20150325 20150326 20150330 20150331 20150401 20150402 20150403 20150406 20150407 20150408 20150409 20150410 20150413 20150414 20150415 20150416 20150417 20150418 20150420 20150421 20150422 20150423 20150424 20150427 20150428

eg Array 2 for :
20150313 20150323 20150324 20150325 20150326 20150327 20150330 20150331 20150401 20150402 20150403 20150406 20150407 20150408 20150409 20150410 20150413 20150414 20150415 20150416 20150417 20150418 20150420 20150421 20150422 20150423 20150424 20150427 20150428 20150313 20150323 20150324 20150325 20150326 20150327 20150330 20150331 20150401 20150402 20150403 20150406 20150407 20150408 20150409 20150410 20150413 20150414 20150415 20150416 20150417 20150418 20150420 20150421 20150422 20150423 20150424 20150427 20150428

请帮助。

1 个答案:

答案 0 :(得分:1)

要获得set2 - set1,您可以将grep流程替换一起使用:

grep -wFvf <(printf "%s\n" "${arr1[@]}") <(printf "%s\n" "${arr2[@]}")
20150323
20150327
20150323
20150327

并将结果存储在另一个数组中:

arr3=($(grep -wFvf <(printf "%s\n" "${arr1[@]}") <(printf "%s\n" "${arr2[@]}")))

PS:set1 - set2使用:

grep -wFvf <(printf "%s\n" "${arr2[@]}") <(printf "%s\n" "${arr1[@]}")