Wc

From LQWiki
Jump to navigation Jump to search

wc (word count) is a command that tells us something about the contents of a file. For example:

$ wc main.c 
     63     140    1328 main.c

This tells us that main.c contains 63 lines, 140 words and 1328 characters. The -L switch to wc gives us the length of the longest line in the input file.

A real-world example of a shell script that sends a mail consisting of some data in a file, provided there are any data in the file.

#!/bin/sh

lines=`wc -l committ-log | sed 's/ //g'`
if test $lines != "0"; then
        current_date=`date '+%b-%d-%T-%Y' | sed 's/ /_/g'`
        < committ-log mail -s "[committ-log for project ABC] \
            $current_date" your@mailaddress.com
        rm committ-log; 
fi;

This script is used to notify me and other people involved in project ABC if something has been committed into our cvs. But since we don't want any unnecessary spam we only send mail when there actually is something in the log file.

Provided by

Most (all?) Linux distributions incorporate this from the GNU Coreutils: and use its man page


Related commands

These are related to summarizing the contents of files.