// shall I write some keywords here to boost search engine ranking?

Saturday, January 15, 2022

Command to Compare Data Files: comm

`diff` is great command to compare the changes among 2 versions of a file, but for comparing data file and extract data from it, `comm` command is more suitable for the job.

For demo purpose, we will prepare 2 sorted data file as below:

left.txt:


123
456
789

right.txt:


789
888
999

`comm` command work by suppressing lines that exist in one of the file, or both file, where -1 suppress lines unique to the left file, -2 suppress lines unique to the right file, and -3 suppress lines appear in both files.

if no options being specify, it will display data in 3 columns:

> comm left.txt right.txt
123
456
                789
        888
        999

To show only lines that unique to left file, we shall suppress the line unique to right file, and line that appear in both files:

> comm -23 left.txt right.txt
123
456

For showing lines that only appear on the right, we will surpress line unique to the left file, and those appear in both files:

> comm -13 left.txt right.txt
888
999

To extract lines that appear in both files, just suppress line that unique to left and right file:

> comm -12 left.txt right.txt
789

And to exlcude only line that appear on the left, command below will show data in 2 column, one of it is the line commons for both files:

> comm -1 left.txt right.txt
        789
888
999

We believe by now you shall be able to use `comm` command for comparing data file effectively. Thanks for reading.

No comments: