Counting the number of lines, words, and characters in a file
Counting the number of lines, words, and characters from a text file are very useful for text manipulations. In several cases, these counts are used in indirect ways to perform some hacks in order to produce the required output patterns and results. This book includes some tricky examples in other chapters. Counting LOC (Lines of Code) is a very important application for developers. We may need to count special types of files excluding unnecessary files. A combination of wc
with other commands help to perform that.
wc
is the utility used for counting. It stands for word count
. Let us see how to use wc
to count lines, words, and characters.
How to do it...
We can use various options for wc
to count the number of lines, words, and characters:
Count the number of lines in the following manner:
$ wc -l file
To use
stdin
as input, use the following command:$ cat file | wc -l
Count the number of words as follows:
$ wc -w file $ cat file...