如何通过Unix中的数据获得与组的验证计数 [英] How to get the validate the count with the group by data in unix
问题描述
我有如下记录列表
来源:
a,yes
a,yes
b,No
c,N/A
c,N/A
c,N/A
d,xyz
d,abc
d,abc
输出:
a, Yes 2
b, No 1
c, N/A 3
d, xyz 1
d, abc 2
c, N/A "File is not correct"
这里'Yes'和'No'是可接受的单词,如果单个$ 1值的任何其他单词计数大于'Yes'或'No'单词计数,则我们发出类似"file is not好"
Here 'Yes' and 'No' are the acceptable words, If any other word count is greater than the 'Yes' or 'No' word count for an individual $1 value then we have issue a statement like "file is not good"
我尝试了以下脚本
awk -F, '{a[$1]++;}END{for (i in a)print i, a[i];}' filetest.txt
推荐答案
如果您不担心输出顺序(与Input_file相同),则可以通过以下方法帮助您.
If you are not worried about the output sequence(same as Input_file) then following may help you in same.
awk -F, '{array[$1", "$2]++;} /yes/{y++;next} /No/{n++;next} /N\/A/{count++;next} END{;for(i in array){printf("%s %s%s\n",i,array[i],(count>y && count>n) && i ~ /N\/A/?RS i" File is not correct":"")}}' Input_file
编辑:现在也添加非一个线性形式的解决方案.
Adding a non-one liner form of solution too now.
awk -F, '{
array[$1", "$2]++;
}
/yes/{
y++;
next
}
/No/{
n++;
next
}
/N\/A/{
count++;
next
}
END{;
for(i in array){
printf("%s %s%s\n",i,array[i],(count>y && count>n) && i ~ /N\/A/?RS i" File is not correct":"")
}
}' Input_file
EDIT2 :由于不应该对OP N/A进行硬编码,因此以下代码将检查是",否"和其余第二个字段的计数.然后,它将根据OP的要求将行数打印出来,并用yes和no进行比较.
As per OP N/A shouldn't be hardcoded then following code will check count of string yes, count of string no and count of rest of the second fields. Then it will compare count of rest with yes and no, based on that it will print the lines as per OP's request.
awk -F, '{
array[$1", "$2]++;
}
/yes/{
y++;
next
}
/No/{
n++;
next
}
{
count[$2]++;
}
END{
for(i in count){
val=val>count[i]?val:count[i]
};
for(i in array){
printf("%s %s%s\n",i,array[i],(val>y && val>n) &&(i !~ /yes/ && i !~ /No/)?RS i" File is not correct":"")
}
}' Input_file
运行完上面的代码后,我开始关注.
After running above code I am getting following.
./script.ksh
d, xyz 1
d, xyz File is not correct
c, N/A 3
c, N/A File is not correct
b, No 1
a, yes 2
d, abc 2
d, abc File is not correct
这篇关于如何通过Unix中的数据获得与组的验证计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!