LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 03: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, 06:47 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,460

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
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, 07:02 AM   #3
ghostdog74
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.
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, 07:44 AM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,949
Blog Entries: 46

Rep: Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182
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 11:50 AM
Shell script: how to find a file containing a string guarriman General 2 08-27-2007 03:39 AM
command or shell script to print line range from file minil Programming 3 12-28-2005 08:05 AM
macro to repeat a token/string n number of times saravkrish Programming 11 05-24-2005 01:31 AM
Print line number X of a file (in shell) rheza Programming 4 01-04-2005 05:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:56 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration