finding common numbers (contents) across 2 or 3 files

I have 3 files which are tab delimited and have numbers in it.

file 1
1
2
3
4
5
6
7

File 2
3
5
7
8

File 3
1
2
3
5
9

The contents in each file is sorted from lowest to highest

I want to compare each of the files together and get the common numbers across 3 files

Resultant file
3
5

My files have large amount numbers and it would be great if I could do this comparison using awk or sed in such a way that I could compare as many files I want and out put the common number to another file

awk' comand file 1 file outputfile
awk' command file 1 file 2 file 3 outputfile

Please let me know.
LA

Try:

x=`cat file_1`; for i in file_2 file_3; do x=`comm -12 $i <(echo "$x")`; done; echo "$x"

assuming all the numbers are unique within a given file:

nawk '{a[$0]++} END{for (i in a) if (a==ARGC-1) print i}' file1 file2 file3 ... fileN

Thanks. It worked.

I would like to know how to modify the above awk comand if I have more than one column in the above files but still find the common numbers only in column 1 across all files.
For example: Column 1 for file 1 file 2 file3 are the same as before but now file 1 has 4 columns, file 2 has 3 columns and file 3 has 1 column.

Please let me know

vgersh's code modified to find the first column, regardless of the number of columns.

nawk '{a[$1]++} END{for (i in a) if (a==ARGC-1) print i}' file1 file2 file3 ... fileN
1 Like