bash值比较并获得uniq结果

时间:2016-04-27 21:34:44

标签: bash comparison

我有以下情况

$ cat a.txt
AAAA-A000 rootvg
AAAA-B001 rootvg
AAAA-B002 rootvg
AAAA-A002 appvg
AAAA-A003 appvg
$ cat b.txt
AAAA-B003 rootvg
AAAA-B004 appvg

我想知道例如rootvg的最后一列中的uniq值是否在第一列中包含不同的值。差异定义为AAAA-A ...和AAAA-B ......并且可能是n次。

对于a.tx

,结果应如下所示
    rootvg ok # because in the first column are values with AAAA-A... AND AAAA-B...
    appvg notok # because in the first column are only values with AAAA-A...
for b.txt
     rootvg notok # because in the first column are only values with AAAA-B...
     appvg notok # because in the first column are only values with AAAA-B...

任何想法,如何在bash中实现这一点?

由于

1 个答案:

答案 0 :(得分:0)

使用gawk可以在关联数组中创建关联数组:

[
  appvg: ["AAAA-A": 1],
  rootvg: ["AAAA-A": 1, "AAAA-B": 1],
]

然后计算内部数组的长度(["AAAA-A": 1]):

% awk '{
  a[$2][substr($1, 1, 6)] = 1
}
END{
  for(key in a) {
    print key, (length(a[key]) > 1 ? "ok" : "notok")
  }
}' <<< "AAAA-A000 rootvg
AAAA-B001 rootvg
AAAA-B002 rootvg
AAAA-A002 appvg
AAAA-A003 appvg"
appvg notok
rootvg ok