Compare two files and display the count of duplicates occurences of a string Compare two files and display the count of duplicates occurences of a string shell shell

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 array a with key as $1 and value as and incrementing count and then skip to next record
  • {...} - Execute this block for 2nd input file
  • for (i in a) Iterate array a
  • {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 array a. Otherwise print the count from array a.