Unix/Linux Go Back    


Shell Programming and Scripting 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

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-26-2009   -   Original Discussion by mrchilly
mrchilly's Unix or Linux Image
mrchilly mrchilly is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 16 April 2011, 6:06 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 10-26-2009   -   Original Discussion by mrchilly
TonyLawrence's Unix or Linux Image
TonyLawrence TonyLawrence is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 1 April 2010, 2:17 PM EDT
Location: SE Mass
Posts: 193
Thanks: 0
Thanked 0 Times in 0 Posts
I think instead you want something to count occurences of unique words, That would be awk or perl, not grep.
Sponsored Links
    #3  
Old Unix and Linux 10-26-2009   -   Original Discussion by mrchilly
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 14 December 2017, 3:02 AM EST
Location: Amsterdam
Posts: 11,652
Thanks: 518
Thanked 3,385 Times in 2,984 Posts
OK, I'll bite Linux:

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 07:49 PM..
    #4  
Old Unix and Linux 10-27-2009   -   Original Discussion by mrchilly
mrchilly's Unix or Linux Image
mrchilly mrchilly is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 16 April 2011, 6:06 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thx for the replies.

Since in a newbie to Linux i hardly understand scrutinzers answer, although it's a very impressive command line LinuxLinux

@Tony: have you got an example of how i would locatie the words that exist the most?
Sponsored Links
    #5  
Old Unix and Linux 10-27-2009   -   Original Discussion by mrchilly
thegeek's Unix or Linux Image
thegeek thegeek is offline
Read Only
 
Join Date: Apr 2009
Last Activity: 30 July 2012, 5:38 AM EDT
Location: /usr/bin/vim
Posts: 946
Thanks: 13
Thanked 38 Times in 36 Posts
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];

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Use to awk to match pattern, and print the pattern grossgermany Shell Programming and Scripting 6 09-23-2009 02:49 PM
want to pattern match using awk asirohi Shell Programming and Scripting 3 08-06-2009 01:25 PM
pattern match tostay2003 Shell Programming and Scripting 3 12-31-2008 01:47 PM
Pattern match in grep tipi Shell Programming and Scripting 4 08-25-2008 07:56 PM
how to use pattern match with grep rei UNIX for Dummies Questions & Answers 5 01-05-2007 04:33 AM



All times are GMT -4. The time now is 04:05 AM.