In some cases you want to find out the number of lines and or words in a document. In my case I do sometimes need to know the number of lines in a file (or datastream). This can be handy when you do some research on log files for example.
Linux is providing a great standard command for this called wc which stands for Word Count.
As an example you can do count of lines with wc on your apache access log with the following command:
wc -l access.log
Which will result in the following: "479 access.log"
Several options are provided:
-c will print the byte count
-m will print the character count
-l will print the lines counted
-w will print the words in the file
The man file of wc will give you some more options that can be usefull. Not only you can use it directly on a file you can also pipe data to it. For example if I would like to know how maney times a certain IP address is logged in my apache log file I could do the following:
to get the data we do a:
we do however want to filter the result to only lines having the IP 22.214.171.124 so we pipe the result from cat to a grep command:
cat access.log | grep 126.96.36.199
This however will provide us a list instead of a number of lines, to do this we can use wc -l . We pipe the result of the previous command to it and we end up with the following line:
cat access.log | grep 188.8.131.52 | wc -l
This will provide ONLY the number of lines as a result. Now the example of counting the lines in my access.log file is only a simple example. The number of options and ways you can use wc is endless.