![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| stop unix find on a directory structure after finding 1st occurrence | jm0221 | Shell Programming and Scripting | 3 | 06-06-2008 07:19 PM |
| To find pid from port number | gmat | HP-UX | 6 | 12-31-2007 07:43 AM |
| how to find the number of files | harish409 | UNIX for Dummies Questions & Answers | 5 | 10-09-2007 09:09 AM |
| how to find serial number | chomca | AIX | 3 | 05-26-2006 10:00 AM |
| How to find number of processes ? | ArabOracle.com | SUN Solaris | 2 | 02-14-2006 03:29 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
||||
|
optimizing - to find the number of occurrence
Hi,
I need to find the number of occurrence of string in a file, for ex: Code:
>cat filename abc abc def ghi ghi ghi ghi abc abc abc 4 def 1 ghi 4 the perl code given below works perfectly. But am sure its not really efficient, any tips/pointers for that. ![]() Code:
#! /opt/third-party/bin/perl
my $file1 = $ARGV[0];
open(fh, $file1) || die "Unable to open the file : $file1 <$!>";
@ele = ();
@count = ();
$index = 0;
$change = 0;
while( chomp($fileContent = <fh>) )
{
$reccnt++;
$index = 0;
$change = 0;
foreach (@ele) {
if( $_ eq $fileContent ) {
$count[$index]++;
$change = 1;
last;
}
$index++;
}
if( $change eq 0 ) {
push(@ele, $fileContent);
push(@count, 1);
}
}
$index=0;
foreach (@ele) {
print "$_####$count[$index]\n";
$index++;
}
exit 0
|
|
||||
|
thanks a lot Per!
that worked with associative arrays! Code:
#! /opt/third-party/bin/perl
open(FH, shift) || die "Unable to open the file : $file1 <$!>";
while( chomp($fileContent = <FH>) )
{
$counts{$fileContent}++;
}
foreach $word (sort keys %counts) {
print "$word###$counts{$word}\n";
}
thanks a lot once again ![]() |
|
|||||
|
perl will close the file when it exits but I guess if you put that close between the loops it might make sense.
And when you're working in perl you don't call associative arrays "associative arrays". You call associative arrays "hashes". And when you write a perl routine you always try to sneek them in where ever you can. This book says: "Until you start thinking in terms of hashes, you aren't really thinking in Perl." |
|
||||
|
If you have Python, here's an alternative:
Code:
#!/usr/bin/python
data = open("data.txt").readlines() #get data into array
data = [i.strip() for i in data] #strip newlines for each element
unique = set(data) #get unique elements
for element in unique:
print "%s has %i counts." % ( element, data.count(i) )
Code:
abc has 4 counts. ghi has 4 counts. def has 1 counts. |
|
||||
|
Unix command
Hi,
I suppose the following simple unix command pipe will do the work: cat filename | sort | uniq -c | sort -nr In case of more than one word in a line, the pipe would change to: cat filename | deroff -w | sort | uniq -c | sort -nr Hope this is what you intended for. (i suspect you would have demanded only perl solutions to this issue )Thanks Srini Last edited by srinivasan_85; 01-09-2007 at 08:30 AM.. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| perl, perl shift, shift, shift perl |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|