Previous Table of Contents Next


Module 154
wc

DESCRIPTION

The external wc command counts the characters, words, and lines in a file and displays the output on your screen. The word delimeters for wc are space, tab, and new-line. The delimeter for a line is new-line. The input may be the standard input, therefore wc is a filter.

The features of wc are:

*  Counts the number of characters in a file.
*  Counts the number of words in a file.
*  Counts the number of lines in a file.
*  Reads from a file.
*  Reads from the standard input.
*  Writes to standard output.
*  Handles multiple files, provides counts for each file read.

COMMAND FORMAT

Following is the general format of the wc command.

    wc [ -clw ] [ file_list ]

Options

The following options may be used to control how wc functions.

-c Count and display only the number of characters contained in the given files.
-l Count and display only the number of lines contained in the given files. Lines are defined as ending with new-lines.
-w Count and display only the number of words contained in the given files. Words are defined as ending with spaces, tabs, or new-lines.
If no options are specified, wc returns the count for lines, words, and characters. Thus wc assumes the -lwc options.
The counts are displayed in the order the options are given. For instance, if you specify -cwl, wc displays characters, words, and lines followed by the filename.

Arguments

The following argument may be passed to the wc command.

file_list One or more files to be counted. The requested counts will be displayed. Each filename will be displayed on a line by itself, preceded by the appropriate counts.
If no files are given, then wc reads from the standard input, which may be a pipe, indirection, or your keyboard.

APPLICATIONS

You use wc to show the size of a file. It is commonly used to count how many lines are in a text document. It may be useful when handling flat ASCII files that are databases. Using wc, you can list how many entries exist in your database. It is also used to count the entries in a directory. The basic rule is, if it's ASCII text and you want to know how many lines, words, or characters are in a file, you use the wc command.

TYPICAL OPERATION

In this activity you use the wc command to count the lines in a file and the number of entries in a directory. Begin at the shell prompt.

1.  Type wc /etc/group and press Return. Your screen resembles the following display:
    cj> wc /etc/group
       12   12   1172    /etc/group

Notice 12 lines and 12 words. The delimeters for words in wc are space, tab, and new-line.
2.  Type wc -w < /etc/passwd and press Return. In this situation wc is reading from the standard input. The shell has redirected the standard input to be the /etc/passwd file. Since no filename is known to wc, no filename is displayed on your screen. See the following display:
    cj> wc -w < /etc/passwd
       363
3.  To display the number of entries in your HOME directory, type ls | wc -l and press Return. Notice the display is different, no filename is displayed. This is because wc read from the standard input, thereby not having a filename to display.
4.  Turn to Module 29 (SV), Module 20 (BSD) to continue the learning sequence.


Previous Table of Contents Next