Linux / UNIX Display Lines Common in Two Files

Linux / UNIX Display Lines Common in Two Files

To display the lines that are common to two files in Linux or Unix, you can use the comm command. This command compares two sorted files and displays the lines that are common to both files, as well as the lines that are unique to each file.

To use the comm command, specify the names of the two files you want to compare as arguments. For example:

refer‮l:ot ‬autturi.com
comm file1 file2

This will display the lines that are common to both file1 and file2, as well as the lines that are unique to each file. The lines that are common to both files will be preceded by a space, and the lines that are unique to each file will be preceded by a tab.

You can also use the -1 and -2 options to suppress the output of the lines that are unique to each file. For example, to display only the lines that are common to both file1 and file2, you can use the following command:

comm -12 file1 file2

This will display the lines that are common to both file1 and file2, but it will not display the lines that are unique to each file.

You can also use the -3 option to suppress the output of the lines that are common to both files. For example, to display only the lines that are unique to file1 and file2, you can use the following command:

comm -3 file1 file2

This will display the lines that are unique to file1 and file2, but it will not display the lines that are common to both files.

Note that the comm command requires that the files be sorted in lexicographic order. If the files are not sorted, the comm command may not produce the expected results. You can use the sort command to sort the files before running the comm command.

Created Time:2017-10-16 14:38:52  Author:lautturi