Это не решение perl
, но оно прекрасно работает с awk
:
AWK 1-вкладыш :
awk 'BEGIN{FS=OFS=",";print "Account,Template,Active,NotActive"}NR>1{if($3=="Y"){a[$2 FS $1]++}else{b[$2 FS $1]++}}END{for(i in a){print i OFS a[i] OFS b[i]+0}for(u in b){if(b[u] && !a[u]){print u OFS a[u]+0 OFS b[u]}}}' input_file | sort -n
Скрипт AWK :
# BEGIN rule(s)
BEGIN {
FS = OFS = "," #defines input/output field separator as ,
print "Account,Template,Active,NotActive" #print the header
}
# Rule(s)
NR > 1 { # from the 2nd line of the file
if ($3 == "Y") { # if the 3rd field is at Y
a[$2 FS $1]++ #increment the array indexed by $2 FS $1
} else {
b[$2 FS $1]++ #do the same when N with the other array
}
}
# END rule(s)
END {
for (i in a) { # loop on all values of the arrays and print the content
print i OFS a[i] OFS (b[i] + 0)
}
for (u in b) {
if (b[u] && ! a[u]) { # do the same with the nonactive array and avoid double printing
print u OFS (a[u] + 0) OFS b[u]
}
}
} #pipe the output to a numerical sort to perform the proper ordering of the output
DEMO:
Введите:
$ cat input_file
Template,Account,Active
123456,123,N
123456,456,Y
321478,456,Y
123456,123,N
321478,456,Y
123457,125,N
123457,125,Y
выход:
Account,Template,Active,NotActive
123,123456,0,2
125,123457,1,1
456,123456,1,0
456,321478,2,0