Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Grep to match unknown pattern

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 10-26-2009
Grep to match unknown pattern

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
# 2  
Old 10-26-2009
I think instead you want something to count occurences of unique words, That would be awk or perl, not grep.
# 3  
Old 10-26-2009
OK, I'll bite :

cat infile:
Code:
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

Code:
$> grep $(grep -o '\b[^[:space:]]*\b' infile |awk '{A[$1]++}END{for (i in A) print A[i],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:
Code:
$> grep -o '\b[^[:space:]]*\b' infile |awk '{A[$1]++}END{for (i in A) print A[i],i}'|sort -n|tail -1|cut -d ' ' -f2
summer


Last edited by Scrutinizer; 10-26-2009 at 06:49 PM..
# 4  
Old 10-27-2009
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?
# 5  
Old 10-27-2009
The word that exist the most..

quick version could be this.. in perl.

Code:
while(<>)  {
    @arr = split /\b/, $_;
    foreach (@arr)  {
        $hash{$_}++ if not ( /\s+/ || /,/ );
    }
}

@arr = sort { $hash{$b} <=> $hash{$a} } keys %hash;
print $arr[0];

Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep everything between two pattern if match not found jhonnyrip Shell Programming and Scripting 1 12-17-2015 11:47 AM
Pattern match using grep between two files mailvkjain Shell Programming and Scripting 1 01-12-2014 03:50 AM
Grep with unknown pattern? Morridini Shell Programming and Scripting 2 01-09-2013 08:31 AM
unable to match grep pattern Jcpratap Shell Programming and Scripting 1 05-28-2012 08:35 AM
awk or grep to match # of words before and after pattern chipperuga Shell Programming and Scripting 1 01-25-2012 04:33 AM
grep - match files containing minimum number of pattern matches stumpyuk Shell Programming and Scripting 3 01-12-2012 11:45 PM
Using grep to print just the pattern match muzzaw Solaris 5 12-03-2009 02:36 AM
Lynx Grep Pattern Match 2 conditions Print from Start to End kkiran UNIX for Dummies Questions & Answers 8 11-24-2009 02:13 PM
Pattern match in grep tipi Shell Programming and Scripting 4 08-25-2008 06:56 PM
how to use pattern match with grep rei UNIX for Dummies Questions & Answers 5 01-05-2007 03:33 AM


All times are GMT -4. The time now is 03:27 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password