The
wc
command is used to count the number of lines, words, and characters in a file.
Observe what happens when you run the
wc
command on the following file.
$ cat letter
Dear Mom,
Final exams are coming up next week.
Please send food, clothes, and
money.
Love,
Susan
$ wc letter
9 17 101 letter
The
wc
command tells you that this file has 9 lines, 17 words, and 101 characters.
You can display only the number of lines by using the
–l
option, the number of words by using the
–w
option, or the number of characters by using the
–c
option.
Here is the
wc
command using the
–l
option:
$ wc –l letter
9 letter
Saving the line count in a variable
To save the number of lines to a variable, embed the wc
command in an assignment statement. -
The next lesson demonstrates sorting the content of a text file using the sort
command.
WC Command - Exercise