TO find the word which occurs maximum number of times


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting TO find the word which occurs maximum number of times
# 1  
Old 01-08-2008
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  
Old 01-08-2008
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  
Old 01-08-2008
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  
Old 01-08-2008
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  
Old 01-08-2008
Klashxx's solution is a good one, thanks :-)
# 6  
Old 01-11-2008
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Egrep find word that occurs twice in a row

Hi there I am trying to figure out and understand what the syntax would be to egrep lines that have a word occur twice in a row. the two words obviously should have a space between them and also it has to be case sensitive which I believe grep is by deffault. the closest I have come is... grep... (7 Replies)
Discussion started by: spo_2138
7 Replies

2. Shell Programming and Scripting

Counting number of times content in columns occurs in other files

I need to figure out how many times a location (columns 1 and 2) is present within a group of files. I figured using a combination of 'while read' and 'grep' I could count the number of instances but its not working for me. cat file.txt | while read line do grep $line *08-new.txt | wc -l... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

3. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies

4. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

5. UNIX for Dummies Questions & Answers

how to read how many times same result occurs?

Is there a way to read how many times each result occurs in a list, say the list.txt has the results: a c d a f c a d e and I need to know how many times each of them occurs such as : a=3 c=2 ..etc. (5 Replies)
Discussion started by: Iifa
5 Replies

6. UNIX for Dummies Questions & Answers

how to count number of times each word exist in a file

I'm trying to count the number of times each word in the file exist for example if the file has: today I have a lot to write, but I will not go for it. The main thing is that today I am looking for a way to get each word in this file with a word count after it specifying that this word has... (4 Replies)
Discussion started by: shnkool
4 Replies

7. Shell Programming and Scripting

Print a word specific number of times

Hi All, I wanted to know if there is a shell command to print a word n number of times The Input File is : Cat 4 Bat 3 Zall 1 Kite 2 Output File required is : Cat Cat Cat Cat Bat Bat Bat Zall Kite (4 Replies)
Discussion started by: sam_2921
4 Replies

8. Shell Programming and Scripting

scripting - write a script that will count the number of times a particular word

hello everyone, I'm trying to learn some scripts but i cant get my head around two of them. 1. how can i write a script that will count the number of times a particular word is used in file? 2. how can i make a script that will take me to a web page from unix? if anyone could help it... (3 Replies)
Discussion started by: BigTool4u2
3 Replies

9. Shell Programming and Scripting

Looking for a single line to count how many times one character occurs in a word...

I've been looking on the internet, and haven't found anything simple enough to use in my code. All I want to do is count how many times "-" occurs in a string of characters (as a package name). It seems it should be very simple, and shouldn't require more than one line to accomplish. And this is... (2 Replies)
Discussion started by: Shingoshi
2 Replies

10. Programming

maximum number of times - a file can be opened

Hi All, We can find the maximum of open file descriptors in hold with respect to a process. As default size was 256 (with getrlimit) and the hard limt was 65536 I tried changing the limit to 1024(with setrlimit) successfully changed the limit but still I couldnt have as many open file... (3 Replies)
Discussion started by: matrixmadhan
3 Replies
Login or Register to Ask a Question