Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 12-07-2009, 04:55 AM   #1
LQ Newbie
Registered: Dec 2009
Posts: 7

Rep: Reputation: 0
Question shell script :: how to print string that appeared maximum number of times in file ??

file.txt -->
alpha gamma alpha beta



Desired output -->

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>").
Old 12-07-2009, 07:47 AM   #2
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,439

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

cat file.txt | tr '[:space:]' "\n" | sort | uniq -c |sort -n -r |head -n 1 | awk '{print $2}'
- 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)
1 members found this post helpful.
Old 12-07-2009, 08:02 AM   #3
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
despite guessing that this should be a homework type qns, here's a partial solution. do the finding of max number yourself.
$ 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.
1 members found this post helpful.
Old 12-07-2009, 08:44 AM   #4
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,906
Blog Entries: 43

Rep: Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142Reputation: 3142

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;

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.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script : Finding a duplicate Number from file ? avklinux Programming 8 12-16-2008 12:50 PM
Shell script: how to find a file containing a string guarriman General 2 08-27-2007 04:39 AM
command or shell script to print line range from file minil Programming 3 12-28-2005 09:05 AM
macro to repeat a token/string n number of times saravkrish Programming 11 05-24-2005 02:31 AM
Print line number X of a file (in shell) rheza Programming 4 01-04-2005 06:55 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:25 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration