Module 22 comm

Previous Table of Contents Next


Module 22
comm

DESCRIPTION

The external comm command compares two lists of sorted ASCII data (files). The default output is three columns ; lines only in file1, lines only in file2, and lines in both files. Lines common to both lists or lines only in one of the two lists may be displayed. The two lists may be stored in files or one may be the standard input. The possible output displays can consist of:

   Lines only existing in the first file
   Lines only existing in the second file
   Lines existing in both files
   Any combination of the above

NOTE:    
Remember, when using comm the files must be sorted in ASCII collating sequence. Thus they must also be ASCII files.

COMMAND FORMAT

Following is the general format of the comm command.

 comm [ -123 ] - file2      comm [ -123 ] file1 file2 

The first format is used to compare the standard input to a file (file2). The second compares two existing files. You may find it helpful to use the first format in a pipe.

Options

The following list describes the options that may be used to control how comm functions.

-1 Suppress the displaying of column 1. Lines existing only in file1 are not displayed.
-2 Suppress the displaying of column 2. Lines existing only in file2 are not displayed.
-3 Suppress the displaying of column 3. Lines existing in both files are not displayed.

Arguments

The following list describes the arguments that may be passed to the comm command.

- Read from the standard input. The input is read from your keyboard or a pipe instead of a file.
file1 The first file to read input from and compare against file2.
file2 The second file to read input from and compare against file1.

FURTHER DISCUSSION

Before you can use the comm command you must be sure that the input files are sorted. If they are not, comm will not function correctly. To sort files refer to Module 122.

You may find it useful to combine options to suppress multiple columns from displaying on your screen. For example, you may want to see only lines that exist in the second file. By using the following format,

 cj> comm -13 file1 file2 

you will find which lines of file2 do not exist in file1. This is useful for distinguishing which file has certain data in it.

RELATED COMMANDS

Refer to the cmp , diff , sort , and uniq commands described in modules 20, 33, 122, and 149.

RETURN CODES

The return codes of comm may be used to control conditional statements within shell scripts. The following list describes the meaning of each return code.

The files are identical.
1 The files were NOT identical.
2 There was a problem reading a file or with an option you provided.

APPLICATIONS

You can use the comm command to compare two lists of data. The two lists must reside in different files. You can compare for common items existing in both sets of data or for data that exists in one file and not the other.

One example would be to compare two lists of names . One file might contain members of a computer club who are single and a second file might contain members of the same club who work with UNIX. By using the command comm -23 singles computers you can list the singles who do not work with UNIX. Using the -12 option would list members who are single and work with UNIX. And the -13 option would list members who are not single and work with UNIX.

Since it can only function on ASCII files, it is limited to text type files.

TYPICAL OPERATION

In this activity you use the comm command to compare two files for like and dislike information. Begin at the shell prompt.

1.    Type cat > users and press Return or use an editor to create the following file.
2.    Type the following lines, press Return to end each line. Press Ctrl-D on the last line to end the input.
 cj> cat > users      Barbara      David      George      Jill      Scott      Tina      ^D 
3.    Type cat > mgmt and press Return .
4.    Type the following lines, ending each line by pressing Return . Press Ctrl-D on the last line.
 cj> cat > mgmt      Andrew      Barbara      Kathy      Scott      ^D 
5.    Type comm users mgmt and press Return to display the three-column output. Column one contains lines only in the users file. Column two contains lines only in the mgmt file. Column three has lines contained in both files.
 cj> comm   users    mgmt                 Andrew                          Barbara      David      George      Jill                 Kathy                          Scott      Tina 
6.    Type cat users comm -12 - mgmt and press Return to display who is in both files. The - represents the standard input and thus allows comm to read from the pipe.
 cj> cat user  comm -12 - mgmt                 Barbara                 Scott 
7.     Turn to Module 124 to continue the learning sequence.


Previous Table of Contents Next

Copyright Wordware Publishing, Inc.


Illustrated UNIX System V
Illustrated Unix System V/Bsd
ISBN: 1556221878
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Robert Felps

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net