Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

How to count particular words using perl (See related posts)

When "grep -c" doesn't cut it (counts only 1 occurrence per line).

perl -lne '$c++ while /SEARCH_STRING/g; END { print $c; }'


For example, to count the number of times ABCD appears in mysqldump output:

mysqldump my_db | perl -lne '$c++ while /ABCD/g; END { print $c; }'


Modified version of snippet found in http://www.wellho.net/forum/Perl-Programming/Issue-with-Grep.html

You need to create an account or log in to post comments to this site.


Related Posts