LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

Reply
 
Search this Thread
Old 12-07-2009, 04:55 AM   #1
mayankmehta83
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

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

Rep: Reputation: 258Reputation: 258Reputation: 258
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)
 
1 members found this post helpful.
Old 12-07-2009, 08:02 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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.
 
1 members found this post helpful.
Old 12-07-2009, 08:44 AM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,401
Blog Entries: 3

Rep: Reputation: 1483Reputation: 1483Reputation: 1483Reputation: 1483Reputation: 1483Reputation: 1483Reputation: 1483Reputation: 1483Reputation: 1483Reputation: 1483
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.

 
  


Reply


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


All times are GMT -5. The time now is 05:14 AM.

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