比较bash,diff和相同值的三个数组 [英] Compare three arrays in bash, diff and identical values

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

问题描述

此问题是指已回答的问题: 比较/区别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" )

数组之间的对称差异:

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

Array3值:

echo $Array3
key10
key11
key12
key7
key8
key9

仅在Array1中的值:

Values only in Array1:

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

我的问题是,我们如何获取Array1&中的值? Array2但不在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天全站免登陆