linux use comm command to find common lines
linux use comm command to find common lines
comm is one of those underrated tools that most people don’t know exists. When I need to find what’s in both of two files, or what’s only in one of them, comm is faster and cleaner than writing an AWK or Python script. The data must be sorted first — that’s the one gotcha that’ll silently give you wrong results if you miss it.
- Create first file
1
2
3
4
5
6
7
$ vi file1
1
2
3
4
5
- Create second file
1
2
3
4
5
6
$ vi file2
2
3
34
35
- Use comm to find common lines
1
2
3
$ comm -12 file1 file2
2
3
Please make sure that data is sorted in asending order else please use sort command.
1
2
3
4
5
6
7
8
9
10
11
12
13
$ vi file3
23
4
1
2
$ cat file3 | sort -n -o file3
$ cat file3
1
2
4
23
This post is licensed under
CC BY 4.0
by the author.