The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ls - maximum number of files karnan UNIX for Dummies Questions & Answers 6 10-15-2008 02:56 PM
maximum number of arguments matrixmadhan UNIX for Dummies Questions & Answers 9 10-15-2008 02:03 PM
Need to replace the first word of a line if it occurs again in the next line(shell) geeko Shell Programming and Scripting 1 09-25-2007 07:15 AM
maximum number of times - a file can be opened matrixmadhan High Level Programming 3 03-21-2006 08:24 AM
Number of login times ftengcheng AIX 7 08-02-2005 12:35 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-07-2008
Registered User
 

Join Date: Jun 2007
Posts: 80
TO find the word which occurs maximum number of times

Hi Folks !!!!!!!!!!!!!!!!!!!

My Requirement is.............

i have a input file:
501,501.chan
502,502.anand
503,503.biji
504,504.raja
505,505.chan
506,506.anand
507,507.chan

and my o/p should be

chan->3

i.e. the word which occurs maximum number of times in a file should be displayed..

Its Really Urgent!!!!!!!!!!!!!!!!!!!
Reply With Quote
Forum Sponsor
  #2  
Old 01-08-2008
jaduks's Avatar
Registered User
 

Join Date: Aug 2007
Location: Assam,India
Posts: 146
A slight weaker one :-) this will not print 2 names with same max number of occurrence

$ awk -F "." '{arr[$2]+=NF/2} END {for (i in arr) {print i"->",arr[i]}}' maxtime.out| sort -k2 | tail -1
Reply With Quote
  #3  
Old 01-08-2008
Registered User
 

Join Date: Nov 2007
Posts: 85
cut -d'.' -f2 filename|sort|uniq -c|sort|tail -1| awk '{print $2"->"$1}'

Last edited by ranjithpr; 01-08-2008 at 04:12 AM.
Reply With Quote
  #4  
Old 01-08-2008
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 383
Awk solution :

Code:
> cat f1
501,501.chan
502,502.anand
503,503.biji
503,503.biji
503,503.biji
504,504.raja
505,505.chan
506,506.anand
507,507.chan
Code:
>awk  '{      
      a[$NF]++
      if ( a[$NF] >= max )
         max=a[$NF]
      }
  END {
      for ( item in a )
         if ( max == a[item] )
            print item
      }' FS="." f1
biji
chan
Reply With Quote
  #5  
Old 01-08-2008
jaduks's Avatar
Registered User
 

Join Date: Aug 2007
Location: Assam,India
Posts: 146
Klashxx's solution is a good one, thanks :-)
Reply With Quote
  #6  
Old 01-11-2008
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 485
awk solution

Hi,

I am not sure whether this one is more efficient for you.

Please try on your file, any result let all of us here know.

Code:
awk 'BEGIN{FS="."}
{
sum[$2]++
}
END{
for (i in sum)
print i"->"sum[i]
}' file > file.t
sort -t">" +1 -n file.t | tail -1
rm file.t
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 02:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0