|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
|
| News | Books | See also | Recommended Links | 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.
If no option is specified, the default is -lwc (counts lines, words, and bytes.). The -c and -m options are mutually exclusive.
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
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
can usually be rewritten like something along the lines ofsomething | grep '..*' | wc -lor even (if all we want to do is check whether something produced any non-empty output lines)something | grep -c . # Notice that . is better than '..*'(orsomething | grep . >/dev/null && ...grep -qif 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.)
Solaris 9 Reference Manual Collection
wc- display a count of lines, words and characters in a file
wc [-c| -m| -C] [-lw] [file...]
The wc utility 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.
The utility also writes a total count for all named files, if more than one input file is specified.
wc considers a word to be a non-zero-length string of characters delimited by white space (for example, SPACE, TAB ). See iswspace(3C) or isspace(3C).
OPTIONS
The following options are supported:
- -c
Count bytes.
- -m
Count characters.
- -C
Same as -m.
- -l
Count lines.
- -w
Count words delimited by white space characters or new line characters. Delimiting characters are Extended Unix Code (EUC) characters from any code set defined by iswspace().
If no option is specified the default is -lwc (count lines, words, and bytes.)
OPERANDS
The following operand is supported:
- file
A path name of an input file. If no file operands are specified, the standard input will be used.
USAGE
See largefile(5) for the description of the behavior of wc when encountering files greater than or equal to 2 Gbyte ( 231 bytes).
ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of wc: LC_CTYPE, LC_MESSAGES, and NLSPATH.
EXIT STATUS
The following exit values are returned:
- 0
Successful completion.
- >0
An error occurred.
Copyright © 1996-2008 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: September 15, 2008