Finding common value across multiple files containing single column values

后端 未结 4 1195
温柔的废话
温柔的废话 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:25

    This will work whether or not the same number can appear multiple times in 1 file:

    $ awk '{a[$0][ARGIND]} END{for (i in a) if (length(a[i])==ARGIND) print i}' file[123]
    10032
    

    The above uses GNU awk for true multi-dimensional arrays and ARGIND. There's easy tweaks for other awks if necessary, e.g.:

    $ awk '!seen[$0,FILENAME]++{a[$0]++} END{for (i in a) if (a[i]==ARGC-1) print i}' file[123]
    10032
    

    If the numbers are unique in each file then all you need is:

    $ awk '(++c[$0])==(ARGC-1)' file*
    10032
    

提交回复
热议问题