比较两个数组在bash,diff和值相同 [英] Compare two arrays in bash, diff and identical values

查看:159
本文介绍了比较两个数组在bash,diff和值相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题指的是回答问题:
<一href=\"http://stackoverflow.com/questions/2312762/compare-difference-of-two-arrays-in-bash\">Compare/Difference在bash 两个数组

This question refers to the answered question: Compare/Difference of two arrays in bash

让我们两个数组:

Array1=( "key1" "key2" "key3" "key4" "key5" "key6" "key7" "key8" "key9" "key10" "key13" )
Array2=( "key1" "key2" "key3" "key4" "key5" "key6" "key11" "key12" "key13" )

阵列之间的差异对称:

Symetrical Differences between arrays:

Array3=(`echo ${Array1[@]} ${Array2[@]} | tr ' ' '\n' | sort | uniq -u `)

ARRAY3值:

Array3 values:

echo $Array3
key10
key11
key12
key7
key8
key9

值仅在数组1:

echo ${Array1[@]} ${Array3[@]} | tr ' ' '\n' | sort | uniq -D | uniq
key10
key7
key8
key9

值仅在ARRAY2

Values only in Array2

echo ${Array2[@]} ${Array3[@]} | tr ' ' '\n' | sort | uniq -D | uniq
key11
key12

我的问题,我们如何能够得到在数组1&安培值;数组2,但不是在ARRAY3(相同)?
预期结果:

My question, how can we get values that are in Array1 & Array2 but not in Array3 (identical)? Expected result:

key1
key13
key2
key3
key4
key5
key6

感谢您的帮助。

推荐答案

确定一些测试之后,似乎我一直在寻找的答案只是:

OK after few test, it seems the answer I was looking for was just:

echo ${Array1[@]} ${Array2[@]} | tr ' ' '\n' | sort | uniq -D | uniq
key1
key13
key2
key3
key4
key5
key6

这篇关于比较两个数组在bash,diff和值相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆