Count Unix Files

Count, Sort and Compare Unix Files

� March 21, 1994 B-18

Unix has several utilities to help count, sort and compare files. To illustrate these utilities, assume we have two files, phonesA and phonesB. Each contains a list of telephone locals and people’s names:

  phonesA             phonesB
4678 George Smith           4221 Susan Wilson
2870 Bill Anderson           3895 Jan Smythe
3717 Joan Brown           3664 John Lee
4221 Susan Wilson

Counting characters, words and lines

The command wc will count the number of lines, words, and characters in a file. To count the file phonesA, type

wc phonesA

The result is

4        12        75 phonesA

The first number is the number of lines in the file, the second is the number of words and the third is the number of characters.

Use wc to count multiple files by giving several filenames as arguments to the command. For example, to count both phonesA and phonesB, type

wc phonesA phonesB

The result is

4        12        75 phonesA
3         9        51 phonesB
7        21       126 total

wc counts lines, words and characters in each file, and totals each of these three items for the two files.

Options for wc limit its output so that only the number of lines, the number of words or the number of characters is displayed, instead of all three:

-l counts only the number of lines
-w counts only the number of words
-c counts only the number of characters

You may combine two of the options for wc. For example, to count the number of words and the number of characters in phonesB, you would type

wc -wc phonesB

source: http://www.sfu.ca/acs/howtos/b/b-18.htm

This entry was posted in Linux. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>