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>"). |
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}' - 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) |
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 |
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:
:hattip: |
All times are GMT -5. The time now is 10:31 PM. |