Episode 005 -wc

Ever want to know how many lines are in a file? How about how many words are in a file or even how many characters? Well then the “wc” command is just for you.  The “wc” command, short for word count, is a very simple command that will print “new line, word and byte counts for file specified, and a total count for all files combined if more than one file is included.”Trik Android

Consider the following little ditty:

the linux wc command
for those not in then know
stands for word count and
does a lot you should know

It counts lines and words and bytes
producing output on site
quickly giving you the numbers
without any blunders

Executing the following command:

wc poem.txt

Results in the following output:

9 40 215 poem.txt

To break it down:

  • 9 lines
  • 40 words
  • 215 characters

You can generate these values independently using the following options:

  • -c or –bytes = number of bytes in the file(s)
  • -m or –chars = number of characters in the file(s)
  • -l or –lines = number of lines in the file(s)
  • -w or –words = number of words in the file(s)

In most cases -c and -m will produces the same output unless you are working in a multi-byte characters set and using multi-byte characters.

As hinted, the wc command can take multiple files and will even accept input from standard in.  This example is impractical but shows how you can pass standard in to wc:

cat poem.txt | wc
cat poem.txt | wc list.txt –

The first example the output of cat is piped to the wc command producing the same results listed above.  The second command pipes the output of cat to wc but we are also passing a file to wc at the same time so we must use a “-” to indicate that we want to include the output of the cat command.  If the list.txt file contained the following:

apples
dog food
sinkers
minnow trap
rope
milk

The output from the “cat poem.txt | wc list.txt -” would be:

 6     8    46    list.txt
9    40  215   –
15   48  261    total

There is an option in wc to list the character length longest line in a file or list of files:  -L or –max-line-length.  In our poem.txt example:

wc -L poem.txt

The following output is produced:

35 poem.txt

The final option to discuss in the wc command is –files0-from= which is an option to pass a list of files separated by an ascii null character to the wc command. The requirement here is that each file is separated by an ascii null character and nothing else. Simply passing a file containing each file you want to pass to the wc command will not work:

poem.txt
list.txt

This list will not work. You need to create the list with null characters instead of new lines, commas or anything else.  In vim you would have to enter the files like so:

poem.txt^@list.txt^@

You cannot simply enter a “^” and a “@” between each entry, this requires a special key input:  “ctrl-v 10”.  That is press ctrl-v and then enter 10. To then process this list with wc, assuming the file listing the files we want to process is called fileslist.txt:

wc –files0-from=fileslist.txt

Producing the following output:

 9     40     215     poem.txt
6      8        46    list.txt
15  48      261    total

You may also get an error:

wc:
: No such file or directory

If you read the info command for wc you will see an example of using the –files0-from= switch with the find command.

Bibliography:

  • man wc
  • info wc

If the video is not clear enough view it off the YouTube website and select size 2 or full screen.  Or download the video in Ogg Theora format:

Thank you very much

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

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