The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 3.00 average. Display Modes
  #1 (permalink)  
Old 01-05-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
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
>output would be
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
  #2 (permalink)  
Old 01-07-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
I was hoping one of our perl experts would answer this. But it looks like you're stuck with me. Here is my best shot, but I'm still learning perl....
Code:
#! /usr/local/bin/perl

open(FH, shift) || die "Unable to open the file : $file1 <$!>";

while( <FH> )
{
        @words=split;
        foreach $word (@words) {

                print "word =", $word ,  "\n";
                $counts{$word}++;
        }
}
foreach $word (sort keys %counts) {
        print "word = ", $word, "  count = ",  $counts{$word}, "\n";
}
Some notes:
I'm using split so there can be more than one word on an input line. It is a perl standard that file handles be upper case so I did that. perl guys don't initialize variables so, for now, I am doing it that way. perl guys don't call exit usually either.
  #3 (permalink)  
Old 01-08-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
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";
}
My requirement should consider a line entry as a word and not to split, so I had removed 'split' functionality

thanks a lot once again
  #4 (permalink)  
Old 01-08-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
i missed out something important,

Code:
close(FH);
  #5 (permalink)  
Old 01-08-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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."
  #6 (permalink)  
Old 01-08-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
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) )
output:
Code:
abc has 4 counts.
ghi has 4 counts.
def has 1 counts.
  #7 (permalink)  
Old 01-09-2007
srinivasan_85 srinivasan_85 is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 28
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
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0