检查值并检查awk是否存在差异

时间:2019-03-27 00:26:13

标签: bash 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
 }

2 个答案:

答案 0 :(得分:0)

我想您要尝试的是仅打印第二列中的值与上一行相比已更改的行。不过,这可能不是您想要的,请在问题中包含正确输出的示例。

您可以使用awk变量(在这种情况下为p)并将其设置为$2的值,这是处理行时的最后一件事。

第一个模式p != $2 { print }意味着我们检查当前行的第二个字段是否等于p,即前一行的值。

awk 'p != $2 { print } { p = $2 }' /tmp/data.txt

即给出以下内容(/tmp/data.txt的内容)

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 

我假设你想生产

0 20.00GB
8 21.00GB
16 22.00GB
24 23.00GB
0 44.00GB

答案 1 :(得分:0)

以下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"
  }
}