Hi there
I would like to search a file for a certain pattern, but i don't know the exact pattern i need to search for.
What i do know is that i need to search for the pattern that exists the most in a certain file.
so if a file looksike this:
summer, summer, winter, spring, summer summer.
i would need a grep command to display summer, while summer is the pattern that exists the most.
regards chilly
I think instead you want something to count occurences of unique words, That would be awk or perl, not grep.
OK, I'll bite :rolleyes::
cat infile:
Hi there
I would like to search a file for a certain pattern, but i don't know the exact pattern i need to search for.
What i do know is that i need to search for the pattern that exists the most in a certain file.
so if a file looksike this:
summer, summer, winter, spring, summer summer.
i would need a grep command to display summer, while summer is the pattern that exists the most.
regards chilly
$> grep $(grep -o '\b[^[:space:]]*\b' infile |awk '{A[$1]++}END{for (i in A) print A,i}'|sort -n|tail -1|cut -d ' ' -f2) infile
summer, summer, winter, spring, summer summer.
i would need a grep command to display summer, while summer is the pattern that exists the most.
or do you mean:
$> grep -o '\b[^[:space:]]*\b' infile |awk '{A[$1]++}END{for (i in A) print A,i}'|sort -n|tail -1|cut -d ' ' -f2
summer
Thx for the replies.
Since in a newbie to Linux i hardly understand scrutinzers answer, although it's a very impressive command line
@Tony: have you got an example of how i would locatie the words that exist the most?
The word that exist the most..
quick version could be this.. in perl.
while(<>) {
@arr = split /\b/, $_;
foreach (@arr) {
$hash{$_}++ if not ( /\s+/ || /,/ );
}
}
@arr = sort { $hash{$b} <=> $hash{$a} } keys %hash;
print $arr[0];