Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Google   


Unix wc command

News Books See also Recommended Links

Options

Examples Reference
Shells Pipes AWK Perl grep find Regex
sort uniq cut tee History Humor Etc

The wc command stands for "word count". It reads one or more input files and, by default, writes the number of newline characters, words and bytes contained in each input file to the standard output. Most often used as the last stage of pipes like ps -e | wc -l

If more than one input file is specified, a line of cumulative count(s) for each of specified files as well as is total count for all files is printed. 

    3 rarme.bat
   11 robots.txt
  334 safary.shtml
 2993 strange_files.txt
    1 today.lst
   18 topbooks.htm
   37 topupdates.htm
   38 topvisited.htm
  124 truncate2.pl
   15 update.bat
    1 xcopy_sp.sh
18791 total

When an option is specified, wc only reports the information requested by  that option.    Option -w counts words. Historically, the wc  defines a word as a ``maximal string of characters delimited by <space>, <tab> or <newline> characters''.  Such a definition creates problem with non-printable characters. Most modern implementations defines a ``word'' in terms of the isspace(3) function, as required by IEEE Std1003.2-1992 (``POSIX.2'').

The default behavior presuppose options -clw having been specified.

Options

The following options are supported:
  1. -c Counts bytes.
  2. -C Same as -m.
  3. -l Counts lines.
  4. -m Counts characters.
  5. -w Counts words delimited by white space characters  -- Extended Unix Code (EUC) characters from any code set defined by iswspace().

If no option is specified, the default is -lwc (counts lines, words, and bytes.). The -c and -m options are mutually exclusive.

Examples


who | wc -l

 counts the number of users logged

ps -e | wc -l

counts the number of processes

ls -l | grep ^d | wc -l  # finds the number of subdirectories in the current directory.

wc -l /etc/passwd

 tells you the number of lines (accounts) in the /etc/passwd file.

wc -w readme.txt

 counts the number of words in the file named readme.txt

Old News ;-)

[Feb 10, 2007] Useless Use of wc Award

Useless Use of wc -l

This is my personal favorite. There is actually a whole class of "Useless Use of (something) | grep (something) | (something)" problems but this one usually manifests itself in scripts riddled by useless backticks and pretzel logic.

Anything that looks like

	something | grep '..*' | wc -l
can usually be rewritten like something along the lines of
	something | grep -c .   # Notice that . is better than '..*'
or even (if all we want to do is check whether something produced any non-empty output lines)
	something | grep . >/dev/null && ...
(or grep -q if your grep has that).

If something is reasonably coded, it might even already be setting its exit code to tell you whether it succeeded in doing what you asked it to do; in that case, all you have to check is the exit code:

	something && ...

I used to have a really wretched example of clueless code (which I had written up completely on my own, to protect the innocent) which I've moved to a separate page and annotated a little bit. It expands on the above and also has a bit about useless use of backticks (q.v.)

 

Reference

Solaris 9 Reference Manual Collection

 

OPTIONS

OPERANDS

USAGE

ENVIRONMENT VARIABLES

EXIT STATUS


Copyright © 1996-2007 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last modified: March 15, 2008