Friday, 9 March 2012

How to find the number of occurance of a character or word in a file

Some times we need to find how many times a character or a word has repeated in file. For this we can use vim. Vim is extremely powerful tool and learning some cool tricks in vim will always help you beat the heat.

So for example, i want to find how many times the word linux comes a file which is full words and lines. Imagine if the file has more than 1000 lines finding manually it will be just waste of time if you know the simple command in the vim.


We can use grep -c linux test.txt, but it will only show the number of lines where the word linux comes.
That's why we are using the vim and the below command.

So for finding the number of occurance of 'linux' in the file called 'test.txt', we can open the file in vim

vim test.txt

Then type the below line and hit ENTER,

:%s/linux//gn 

This will show '6 matches on 2 lines' at the bottom of the vim.

Replace the linux with any other word or character's count that you want to find. That's all.

                                                                                                                                 credit [link]

No comments:

Post a Comment