Home > Blockchain >  Bash script to compare two files and display the count of duplicates saved in another file include i
Bash script to compare two files and display the count of duplicates saved in another file include i

Time:09-21

I'd like to compare file 1 and file 2 using their first columns and four columns the entire line or row from file 2 where they match in file 1 .I'd also like to save the results in a 3rd file, and count the duplicates.

File1:

00:00:00 W1 T Y8.4.237 51934 X1.69 51934 17.203.73.207 #S  
00:00:00 W1 U Y8.1.161 63675 W121 63675 200.47.95.8 10]  
00:00:00 W1 T Y8.42.69 35684 X1.71 35684 2.250.5.106 #S  
00:00:00 Q2 T Y0.244.246 61631 X4.126 61631 3.211.0.248 #S  
00:00:01 W1 U Y8.1.161 63674 W121 63674 200.47.95.18 22]  

File 2:

Y8.4.237  
Y8.1.161  
Y8.42.69   
Y0.244.246   
Y8.1.161

And in file3, I want to include the duplicates and count them.

For example, the result is:

Y8.4.237 :  Total 0
Y8.1.161:   Total 2 
Y8.42.69:   Total 0
Y0.244.246 :Total 0

I use this command but i could not change by dynamically?

awk '{print $4}'  file1.txt |grep  -w -c "Y8.1.161"  

How can I do this? Thank you a lot for your help and efforts

CodePudding user response:

Make an associative array from the contents of file1.txt. Then increment the counts when reading file2.txt.

awk 'FNR==NR { c[$1] = 0; next; } # file2.txt
     $4 in c { c[$4]   } # file1.txt
     END { for (x in c) printf("%s: Total %d\n", x, c[x]) }
' file2.txt file1.txt > result.txt
  • Related