Search code examples
awktext-processingcomm

Finding common value across multiple files containing single column values


I have 100 text files containing single columns each. The files are like:

file1.txt
10032
19873
18326

file2.txt
10032
19873
11254

file3.txt
15478
10032
11254

and so on. The size of each file is different. Kindly tell me how to find the numbers which are common in all these 100 files.

The same number appear only once in 1 file.


Solution

  • awk to the rescue!

    to find the common element in all files (assuming uniqueness within the same file)

    awk '{a[$1]++} END{for(k in a) if(a[k]==ARGC-1) print k}' files
    

    count all occurrences and print the values where count equals number of files.