Finding common value across multiple files containing single column values

后端 未结 4 1196
温柔的废话
温柔的废话 2020-12-11 22:13

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
112         


        
4条回答
  •  有刺的猬
    2020-12-11 22:28

    One using Bash and comm because I needed to know if it would work. My test files were 1, 2 and 3, hence the for f in ?:

    f=$(shuf -n1 -e ?)                     # pick one file randomly for initial comms file
    
    sort "$f" > comms 
    
    for f in ?                             # this time for all files
    do 
      comm -1 -2 <(sort "$f") comms > tmp  # comms should be in sorted order always
      # grep -Fxf "$f" comms > tmp         # another solution, thanks @Sundeep
      mv tmp comms
    done
    

提交回复
热议问题