Sunday, 17 May 2009

count no.of words and each word's occurrence in file(s)

If I want to extract a word that i want and see how many times it occurred out of those number of files,here is one way of doing it in perl .


my %count = ();
while (<>) {
@words = split(' ');
my %count = ();
foreach $word (@words) {
$count{$word}++ if($word =~ /sachin/);
foreach $word (sort keys %count) {
print "$word : occured $count{$word} number of times\n";

Note: <> - the daimond operator reads all the command line arguments (file by file) and split each line into words seperated by space.I have taken a hash to collect the words as my keys and it's num.of occurrence as value and incremented only if it matches word "sachin"

execute : perl cricketers.dat
output : sachin : occured 10 number of times

No comments:

Post a Comment

Tweets by @sriramperumalla