Craig,
A fairly simple way to do this would be to open the file
and step though it tracking the first words like this:
open FH, "the_file.txt";
foreach $line (<FH>) {
# non-greed pattern match to get the
first word on the line
($first_word) =
$line =~ /^(\S+?)\s/; # increment a hash element for that
word
$word_counts{$first_word}++;}
close FH;
# print each element of the hash and how many times it
appeared in the file
foreach (sort keys %word_counts) {
print "$_
$word_counts{$_}\n";
}
Matt
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Sharp, Craig
Sent: Wednesday, April 26, 2006 7:44 AM
To: perl-unix-users@listserv.ActiveState.com
Subject: [Perl-unix-users] Counting words
evaluate eaprod 7844 2 15688 0 2
evaluate agncyis 7347 2 14694 0 1
agncyis compstr 1117 2 0 0 0
evaluate highcost 7036 4 28144 0 0
evaluate adjtrk1 4661 7 9322 0 65
adjtrk1 docinit 14691 7 0 0 0
evaluate doc-type 7864 71 55048 0 32
evaluate allselfemp 7788 2 15576 0 4
as-undstat synctrack 9014 2 9014 0 4
as-undstatdt synctrack 9014 2 0 0 4
as-evtuser synctrack 9014 2 0 0 3
trakinit as-apprlet 1151 1 0 0 7
evaluate fixdprod 7475 26 52325 0 17
evaluate balloon 5786 32 11572 0 15
evaluate arm 5774 16 11548 0 8
evaluate pgyprodcd 9374 6 18748 0 0
evaluate ownr-occ 7762 115 54334 0 71
evaluate ltv 11526 70 92208 0 60
evaluate lowscr 9587 45 86283 0 19
lowscr cond-ck 2499 45 12495 0 19
lowscr compint 1852 45 16668 0 7
evaluate units 11556 27 80892 0 9
evaluate cashout 8603 97 60221 0 76
evaluate helpayoff 9649 2 19298 0 2
helpayoff compstr 1117 2 0 0 0
evaluate purp-ref 8364 2 16728 0 4
opncon access 10508 5 10508 0 2
opncon pkxtrkst 45614 1 45614 0 13
pkxtrkst access 10508 1 0 0 0
pkxtrkst helpmess 5890 23 5890 0 22
opncon helpmess 5890 2 0 0 2
opncon auditrec 8842 1 8842 0 1
I need to loop through and find the first word in each line and count the number of lines that contain this word in the first position. Example using the above list:
Evaluate 17
Agncyis 1
Opncon 4
I need to do this for each unique word in the first positon.
Best regards,
Craig Sharp
Quicken Loans | Unix Engineer
| Direct: 734.805.7839 |
Fax: 734.805.5513
[EMAIL PROTECTED]
www.quickenloans.com
The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose it
to anyone else. If you received it in error please notify us immediately and
then destroy it.
_______________________________________________ Perl-Unix-Users mailing list Perl-Unix-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs