Comparing Common Values between two files of different length (Linux)

Viewed 123

I've compared two files that are of different lengths, for which I first found unique IDs that ARE present in file 1 but not in file 2.

However, now I want to find common unique values between the files, I've seen multiple times the use of the comm command, however, these files are not of the same length.

Example

file 1:

2 
4 
6 
8 
10

file 2:

9
8
7
6
5
4
3
2
1

Desired Output:

2 
4 
6 
8 

For Finding Unique Differences I used the following command:

awk 'FNR==NR {a[$0]++; next} !($0 in a)' file1.sorted file2.sorted > diff_values.txt

For Finding Common Values I have tried using the following command but am not entirely sure if this is the correct approach or if there exist any alternatives:

comm -12 file1.sorted file2.sorted > comm_values.txt
1 Answers

There are many alternatives to using comm, just as there are many alternatives to doing anything in Unix, but comm is the tool designed to do what you're asking for.

Common lines:

$ comm -12 <(sort file1) <(sort file2)
2
4
6
8

Different lines:

$ comm -3 <(sort file1) <(sort file2)
        1
10
        3
        5
        7
        9

Lines only in the first file:

$ comm -23 <(sort file1) <(sort file2)
10

Lines only in the second file:

$ comm -13 <(sort file1) <(sort file2)
1
3
5
7
9

If you want alternatives though, here's some of the many alternative scripts you could consider and adapt to whatever you want to do:

$ awk 'NR==FNR{a[$0]; c[$0]; next} {b[$0]; c[$0]} END{for (i in c) if ((i in a) && (i in b)) print i}' file1 file2
2
4
6
8

$ awk 'NR==FNR{a[$0]; c[$0]; next} {b[$0]; c[$0]} END{for (i in c) if (!((i in a) && (i in b))) print i}' file1 file2
1
3
5
7
9
10

$ awk 'NR==FNR{a[$0]; c[$0]; next} {b[$0]; c[$0]} END{for (i in c) if ((i in a) && !(i in b)) print i}' file1 file2
10

$ awk 'NR==FNR{a[$0]; c[$0]; next} {b[$0]; c[$0]} END{for (i in c) if (!(i in a) && (i in b)) print i}' file1 file2
1
3
5
7
9
Related