对列进行分类并计算列中的警告数量 [英] Categorize a column and count the number of warnings in a column
问题描述
我有一个名为 out.txt 的文件,如下所示:
I am having a file called out.txt as below:
Statement 1 Statement 2 Statement 3 Statement 4
The declaration is not done / Exp / * / This is expected
The declaration is starting/started / St / * / This is not expected
The declaration is not yet designed / Yt / & / This is a major one
The declaration is confirmed / Exp / * / This is okay
The declaration is not confirmed / Ntp / & / This is a major issue
我需要对第3列(声明3)进行归类和归类,如果*是警告,如果是& ,则是错误,如下所示:
I need to sum up and categorize from column 3 (Statement 3), if it is * as Warning and if it is & it is a Error as below:
Out:
Warnings:
Exp : 2
St : 1
Total : 3
Errors:
Yt : 1
Ntp: 1
Total :2
我尝试了以下代码,但未获得确切的输出:
I tried below code, but not getting the exact output:
#!/bin/bash
echo " " ;
File="out.txt"
for z in out.txt;
do
if grep -q "&" $z/"$File"; then
echo "$z:";
awk -F' / '
{ a[$2]++ }
END{ for(j in a){ print j, a[j]; s=s+a[j] };
print "Total :", s}' out.txt
else
echo "$z:";
done
推荐答案
由于OP确认没有错误关键字,因此应由&
关键字放在行的倒数第二个字段中,然后尝试执行以下操作.
Since OP confirmed that there are NO keywords for errors it should be decided by &
keyword in 2nd last field of line then try following.
awk -F'/' '
match($0,/[[:space:]]+\/[^/]*[[:space:]]+\//){
val=substr($0,RSTART,RLENGTH)
gsub(/[[:space:]]+|\//,"",val)
str=$(NF-1)
gsub(/ +/,"",str)
if(str=="&"){
countEr[val]++
}
else{
countSu[val]++
}
val=str=""
}
END{
print "Out:" ORS "Warings:"
for(i in countSu){
print "\t"i,countSu[i]
sumSu+=countSu[i]
}
print "Total:"sumSu
print "Errors:"
for(i in countEr){
print "\t"i,countEr[i]
sumEr+=countEr[i]
}
print "Total:"sumEr
}' Input_file
通用解决方案,可以在变量中给出所有错误名称,然后我们不必像以前的解决方案一样手动将所有条件都放入.您能否根据仅使用GNU awk
编写和测试的示例来尝试以下操作.
Generic solution where one could give all errors names in a variable and then we need NOT to put it all conditions manually like my previous solution does. Could you please try following, based on your shown samples only written and tested with GNU awk
.
awk -v errors="Ntp,Yt" '
BEGIN{
num=split(errors,arr,",")
for(i=1;i<=num;i++){
errorVal[arr[i]]
}
}
match($0,/[[:space:]]+\/[^/]*[[:space:]]+\//){
val=substr($0,RSTART,RLENGTH)
gsub(/[[:space:]]+|\//,"",val)
if(val in errorVal){
countEr[val]++
}
else{
countSu[val]++
}
val=""
}
END{
print "Out:" ORS "Warings:"
for(i in countSu){
print "\t"i,countSu[i]
sumSu+=countSu[i]
}
print "Total:"sumSu
print "Errors:"
for(i in countEr){
print "\t"i,countEr[i]
sumEr+=countEr[i]
}
print "Total:"sumEr
}' Input_file
说明: 添加以上详细说明.
Explanation: Adding detailed explanation for above.
awk ' ##Starting awk program from here.
match($0,/[[:space:]]+\/[^/]*[[:space:]]+\//){ ##Using match function to match space slash space and slash here as per samples to get value.
val=substr($0,RSTART,RLENGTH) ##Saving sub-string into variable val from RSTART to RLENGTH here.
gsub(/[[:space:]]+|\//,"",val) ##Removing spaces and slashes with NULL in val here.
if(val=="Ntp" || val=="Yt"){ ##Checking condition if value is either Ntp PR Yt then do following.
countEr[val]++ ##Increase count for array countEr with 1 with index of val here.
}
else{ ##Else do following.
countSu[val]++ ##Increase count of array countSu with index of val here.
}
val="" ##Nullifying val here.
}
END{ ##Starting END block of this program here.
print "Out:" ORS "Warnings:" ##Printing string Out new line and Warnings here.
for(i in countSu){ ##Traversing through countSu here.
print "\t"i,countSu[i] ##Printing tab index of array and value of CountSu here.
sumSu+=countSu[i] ##Keep on adding value of countSu current item into sumSu variable here.
}
print "Total:"sumSu ##Printing Total string with sumSu value here.
print "Errors:" ##Printing string Errors here.
for(i in countEr){ ##Traversing through countEr here.
print "\t"i,countEr[i] ##Printing tab index i and countEr value here.
sumEr+=countEr[i] ##Keep on adding value of countEr current item into sumEr variable here.
}
print "Total:"sumEr ##Printing Total string with sumEr value here.
}' Input_file ##Mentioning Input_file name here.
这篇关于对列进行分类并计算列中的警告数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!