[How-To] Count how many times a word appears in a file?
Earlier today morning, i was doing some analysis on how the distribution of output random method works for a design i am proposing to a customer.
While, working on the analysis, i came across a situation where i had a massive log file (custom log format) and had to find occurrences of 8 different types of strings to come to a conclusion.
While i cannot give details about the design and the problem i was working on, i definitely can help you with finding how many times a particular word or string appears in a file. The solution below assumes that you have the file and also have access to a UNIX server.
For the sake of clarity, in the example below, i have a file named “test-results1.log” and i want to find out how many times 1.1.1.8 appears in the file. For your own example, you must replace test-results1.log with your filename and 1.1.1.8 with the word/string you wish to find occurences for.
$ tr ‘ ‘ ‘\12′<test-results1.log |grep -cw 1.1.1.8
An example of the above command is shown below. Here i am creating a file called technofriends.log and entering some characters in it. Later, i will find the occurrences of “tes” (without quotes) in the file.
root@mercury:/db/xx/xx] cat technofriends.log
tes tes
dd fff
tes tes zzz
[root@mercury:/db/xx/xx] tr ‘ ‘ ‘\12′<technofriends.log|grep -cw tes
4
[root@mercury:/db/xx/xx]
You can follow me on Twitter at http://twitter.com/vaibhav1981
Do stay tuned to Technofriends for more, one of the best ways of doing so is by subscribing to our feeds. You can subscribe to Technofriends feed by clicking here.
Related posts:
- [Java] JAR2EXE converts a Jar file into an Exe file
- How-To: Read an XML file using DOM parser in JAVA?
- [How-To] Password Protect your Word and Excel Files
- [File Sharing] FileTwt Brings File Sharing To Twitter
- Microsoft Windows: More fun with the HOSTS file