LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script :: how to print string that appeared maximum number of times in file ?? (http://www.linuxquestions.org/questions/linux-newbie-8/shell-script-how-to-print-string-that-appeared-maximum-number-of-times-in-file-773927/)

mayankmehta83 12-07-2009 04:55 AM

shell script :: how to print string that appeared maximum number of times in file ??
 
file.txt -->
alpha gamma alpha beta

<OR>

alpha
gamma
alpha
beta

Desired output -->
alpha

Thanks in advance.

PS: Best would be if one can suggest an in-line shell (bash) script command (i.e. something of type "cat file.txt | <some_command>").

Guttorm 12-07-2009 07:47 AM

There are probably many ways to do it, but here is one:

Code:

cat file.txt | tr '[:space:]' "\n" | sort | uniq -c |sort -n -r |head -n 1 | awk '{print $2}'
Meaning:
- cat file.txt
- convert all whitespace to newlines
- sort it
- check unique lines, and add the word count first
- sort by word count (numerical) reverse
- only use the first line
- print second word (skip the count)

ghostdog74 12-07-2009 08:02 AM

despite guessing that this should be a homework type qns, here's a partial solution. do the finding of max number yourself.
Code:

$ awk '{for(i=1;i<=NF;i++){w[$i]++ } }END{for(o in w) print o,w[o]}' file
<OR> 1
gamma 2
alpha 4
beta 2

Next time, try to come up with your own solution first.

onebuck 12-07-2009 08:44 AM

Hi,

This does smell like homework. Then why do you respond with solutions when the OP doesn't even show what he/she has attempted?
If there were some problems with what the OP presents then addressing that would be appropriate.

The LQ Rules don't state that we cannot help but;

Quote:

excerpt LQ Rules;

Do not expect LQ members to do your homework - you will learn much more by doing it yourself.
The OP will not gain from this other than to provide a solution that he/she did not create or conclude thus not learning.

:hattip:


All times are GMT -5. The time now is 08:39 PM.