Monday, February 05, 2007

Count the Lines, Words and Characters in a File

Counting the number of lines in a file can be useful. I've used them to control a looping construct. Here's an example of a run.

# wc esofthub.dat
10 23 60 esofthub.dat
This file has 10 lines, 23 words and 60 characters.

# wc -lwc esofthub.dat esofthub1.dat
10 23 60 esofthub.dat
12 25 63 esofthub1.dat
22 48 123 total
This command totals the lines, words and characters.

or
This method also totals the lines, words and characters.
# wc *
7 7 38 user1.txt
9 9 47 user2.txt
11 11 66 user3.txt
5 5 31 user4.txt
32 32 182 total

Note: The Blogger application parsed out the less-than characters so an underscore was used to preserve the contiguous nature of the "<<_eof">

# wc <<_EOF
> This will show the
> number of lines, words
> and characters in this
> exercise
> _EOF
4 13 76

No comments: