[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: recursively count the words occurrence in the text files



>> On Thu, 30 Dec 2010 10:34:59 -0800 (PST), 
>> S Mathias <smathias1972@yahoo.com> said:

S> ...recursively count the words occurrence in the text files.

   This assumes words consist of alphanumeric characters only.  If that's
   not the case, you'll need to change the "tr/" line in the script.

   me% head */*
   ==> one/asf.txt <==
   word1 word2 word3

   ==> one/asfcxv.txt <==
   word2 word4, word5

   ==> one/dsgsdg.txt <==
   word1. word2

   ==> three/qwerbdsg.txt <==
   word7, word8 word9 word10

   ==> three/weberg.txt <==
   word4 word3

   ==> three/werdf.txt <==
   asdf, word2

   ==> two/ergd.txt <==
   word6

   ==> two/sdgsddsf.txt <==
   word6, word3


   me% cat words
   #!/usr/bin/perl -w
   use strict;
   my %words;

   while (<>) {
       tr/a-zA-Z0-9/ /cs;
       foreach my $w (split) {
           $words{$w}++;
       }
   }
   foreach (sort keys %words) {
       print "$_ $words{$_}\n";
   }
   exit(0);


   me% ./words */*
   asdf 1
   word1 2
   word10 1
   word2 4
   word3 3
   word4 2
   word5 1
   word6 2
   word7 1
   word8 1
   word9 1

-- 
Karl Vogel                      I don't speak for the USAF or my company
Never take a beer to a job interview.  --Martha Stewart's tips for Rednecks


Reply to: