Free Hosting Online for WorkStations

< Previous | Contents | Next >

comm

The comm program compares two text files and displays the lines that are unique to each one and the lines they have in common. To demonstrate, we will create two nearly identi - cal text files using cat:


[me@linuxbox ~]$ cat > file1.txt a

b c d

[me@linuxbox ~]$ cat > file2.txt b

c d e

[me@linuxbox ~]$ cat > file1.txt a

b c d

[me@linuxbox ~]$ cat > file2.txt b

c d e


Next, we will compare the two files using comm:


[me@linuxbox ~]$ comm file1.txt file2.txt

a

b c d

e

[me@linuxbox ~]$ comm file1.txt file2.txt

a

b c d

e


As we can see, comm produces three columns of output. The first column contains lines unique to the first file argument; the second column, the lines unique to the second file ar- gument; the third column contains the lines shared by both files. comm supports options in the form -n where n is either 1, 2 or 3. When used, these options specify which col- umn(s) to suppress. For example, if we only wanted to output the lines shared by both files, we would suppress the output of columns one and two:


[me@linuxbox ~]$ comm -12 file1.txt file2.txt

b c d

[me@linuxbox ~]$ comm -12 file1.txt file2.txt

b c d


Top OS Cloud Computing at OnWorks: