我兩個數組,說:如何在Bash中獲得兩個數組的聯合?
arr1=("one" "two" "three")
arr2=("two" "four" "six")
會是什麼讓這兩個數組的聯合Bash中的最佳方式?
我兩個數組,說:如何在Bash中獲得兩個數組的聯合?
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+。
不需要'arr3':在$ {arr1 [@]}「」$ {arr2 [@]}「中使用' –
之前bash
4,
while read -r; do
arr+=("$REPLY")
done < <(printf '%s\n' "${arr1[@]}" "${arr2[@]}" | sort -u)
sort -u
在其輸入端執行免費DUP-結合;循環僅將所有內容都放回到數組中。
實際的聯合(沒有愚蠢)或簡單地兩個數組連接? –
實際聯盟(沒有重複) – user2436428
在zsh中,你可以使用'typeset -U arr',很確定在bash中沒有這麼簡單的方法。 – Kevin