Compare two files and display the count of duplicates occurences of a string
Using awk you can do:
awk 'FNR==NR{a[$1]++; next} {print $1 ": Total", ($1 in a)?a[$1]:0}' infoFile.txt main1.txt111: Total 2222: Total 0333: Total 1
How it works:
FNR==NR
- Execute this block for first file only{a[$1]++; next}
- Create an associative arraya
with key as$1
and value as and incrementing count and then skip to next record{...}
- Execute this block for 2nd input filefor (i in a)
Iterate arraya
{print $1 ": Total", ($1 in a)?a[$1]:0}
- Print first field followed by text": Total "
then print 0 if first field from 2nd file doesn't exist in arraya
. Otherwise print the count from arraya
.