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
}
解决方案
以下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.00GB
和44.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"
}
}
推荐阅读
- java - 如何将数字插入已排序的链表
- python - 在 py.test 中是否可以报告测试运行中生成的任意值?
- r - 如何通过R中dplyr中的另一个变量(而不是频率)计算比例
- python - 有没有一种方法可以从另一个文件中的一个文件中查找单词并将在另一个文件中找不到的单词输出到一个新文件中?
- python - Xpath 响应总是返回 null
- python-3.x - 在 Mean() 中除以零?
- javascript - 按钮点击结果:HTML中的“'功能'未定义”
- android - 打开 Activity 时,TalkBack 何时开始?
- python - 为什么缩放然后转换我的火车数据会导致“ValueError:设置带有序列的数组元素”?
- sql - 加入甲骨文