linux - 我们如何在 Bash 中获得两个数组的并集?

标签 linux bash shell

我有两个数组,比如说:

arr1=("one" "two" "three")
arr2=("two" "four" "six")

在 Bash 中合并这两个数组的最佳方法是什么?

最佳答案

首先,组合数组:

arr3=("${arr1[@]}" "${arr2[@]}")

然后,应用来自 this 的解决方案发布以删除重复项:

# Declare an associative array
declare -A arr4
# Store the values of arr3 in arr4 as keys.
for k in "${arr3[@]}"; do arr4["$k"]=1; done
# Extract the keys.
arr5=("${!arr4[@]}")

这假设 bash 4+。

关于linux - 我们如何在 Bash 中获得两个数组的并集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33153756/

相关文章:

linux - Grep 之前和之后最后 '/'

c - linux 使用系统 ("ping") 在守护进程中被阻止

c++ - 一个 vector 在 2500 万个 vector 中的查找距离

mysql - 将 mysqlimport 与文件列的子集一起使用

shell - 如何捕获从 mysqldump 返回的错误并使用它在 shell 脚本中执行另一个操作?

linux - 使用 gnuplot 将命令放在多行中

linux - 无法从 Windows 批处理文件运行 shell 脚本

Linux GUI : gnuplot, bash 脚本,octave

linux - 在启用了地址空间随机化的环境中运行时,程序获得错误结果的可能原因是什么?

bash - 期待脚本问题