This will do the trick:
$ awk '!a[$0]++{c[$1]++;c[$2]++} END{for(k in c){print k" - "c[k];s+=c[k]}print "\nTotal No -",s}' file aka - 1 bga - 2 aab - 1 abb - 2 bbb - 2 Total No - 8
In a more readable form, the script:
!lines[$0]++{ count[$1]++ count[$2]++ } END { for (line in count) { print line" - "count[line] sum += count[line] } print "\nTotal No -",sum }
To run it in this form, save it in a script.awk file and:
$ awk -f script.awk file aka - 1 bga - 2 aab - 1 abb - 2 bbb - 2 Total No - 8
source share