首页 > 解决方案 > 检查值并检查是否与 awk 有差异

问题描述

我试图比较从已运行多次的 CSV 文件中获取的值,这是 csv 文件的一部分

0 20.00GB  
1 20.00GB  
2 20.00GB  
3 20.00GB  
8 21.00GB  
9 21.00GB  
10 21.00GB  
11 21.00GB  
16 22.00GB  
17 22.00GB  
18 22.00GB  
19 22.00GB  
24 23.00GB  
25 23.00GB  
26 23.00GB  
27 23.00GB  
0 44.00GB  

...

我想要做的是对于相同的 id 检查第二列是否有不同的值这就是我目前所拥有的

function checkdif() {
awk -F, '{print $1" "$12}' $1
 }

标签: bashawk

解决方案


以下awk命令将执行您要查找的操作:

对于相同的 id 检查第二列是否具有不同的值

输入1:

$ cat check_id
0 20.00GB  
1 20.00GB  
2 20.00GB  
3 20.00GB  
8 21.00GB  
9 21.00GB  
10 21.00GB  
11 21.00GB  
16 22.00GB  
17 22.00GB  
18 22.00GB  
19 22.00GB  
24 23.00GB  
25 23.00GB  
26 23.00GB  
27 23.00GB  
0 44.00GB 

id =0有 2 个不同的值20.00GB44.00GB

输入2:

$ cat check_id2
0 20.00GB  
1 20.00GB  
2 20.00GB  
3 20.00GB  
8 21.00GB  
9 21.00GB  
10 21.00GB  
11 21.00GB  
16 22.00GB  
17 22.00GB  
18 22.00GB  
19 22.00GB  
24 23.00GB  
25 23.00GB  
26 23.00GB  
27 23.00GB  
0 20.00GB 

所有 id 在第二列中具有相同的值

运行1:

awk '{b[$1]++;if(b[$1]>1 && a[$1]!=$2){print "id: "$1" has 2 different values "a[$1]" and "$2;test=1;exit};a[$1]=$2}END{if(test==0)print "All identical ids have the same value in the second column"}' check_id 
id: 0 has 2 different values 20.00GB and 44.00GB

运行2:

awk '{b[$1]++;if(b[$1]>1 && a[$1]!=$2){print "id: "$1" has 2 different values "a[$1]" and "$2;test=1;exit};a[$1]=$2}END{if(test==0)print "All identical ids have the same value in the second column"}' check_id2
All identical ids have the same value in the second column

可读性命令:

# Rule(s)     
{ 
  #associative array to count the number of occurrences of a specific id    
  b[$1]++
  #when we encounter the element a second time and if the value is different then the previous encountered (stored in a)
  #we print that the ids are different and we stop the execution of awk
  #if you want to print all the ids that have different values then remove the exit
  #the test variable is used to not print the default message in the END clause
  if (b[$1] > 1 && a[$1] != $2) {
    print "id: " $1 " has 2 different values " a[$1] " and " $2
    test = 1
    exit
  }
  #store in the associative array a the pairs id <-> value
  a[$1] = $2
}

# END rule(s)     
END {
  if (test == 0) {
    #if we reach this point it is that all identical ids have the same value in the second column
    print "All identical ids have the same value in the second column"
  }
}

推荐阅读