Compares two sorted files line by line. This command is extremely useful to intersect two files. My most common use case is that I have to files and I want to take a particular column from them and intersect them to see, which entries are present in both columns together.

Intersecting two specific columns of two files

For that, I need first ‘cut’ to extract the columns, then ‘sort’ to sort the lists and both outputs I feed into the ‘comm’ command lie this

comm -12 <(cut -f 3 file1.txt | sort) <(cut -f 3 file2.txt | sort)

Here, I compare the third column (-f 3) of the two files file1.txt and file2.txt

Leave a Reply