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 > 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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need to replace the first word of a line if it occurs again in the next line(shell) geeko Shell Programming and Scripting 4 06-18-2009 02:36 PM
ls - maximum number of files karnan UNIX for Dummies Questions & Answers 6 10-15-2008 05:56 PM
maximum number of arguments matrixmadhan UNIX for Dummies Questions & Answers 9 10-15-2008 05:03 PM
maximum number of times - a file can be opened matrixmadhan High Level Programming 3 03-21-2006 11:24 AM
Number of login times ftengcheng AIX 7 08-02-2005 03:35 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 Rate Thread Display Modes
  #1 (permalink)  
Old 01-08-2008
aajan aajan is offline
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!!!!!!!!!!!!!!!!!!!
  #2 (permalink)  
Old 01-08-2008
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 166
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
  #3 (permalink)  
Old 01-08-2008
ranjithpr ranjithpr is offline
Registered User
  
 

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

Last edited by ranjithpr; 01-08-2008 at 07:12 AM..
  #4 (permalink)  
Old 01-08-2008
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
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
  #5 (permalink)  
Old 01-08-2008
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 166
Klashxx's solution is a good one, thanks :-)
  #6 (permalink)  
Old 01-11-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,079
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
Closed Thread

Bookmarks

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 12:39 AM.


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