Unix: list out Pattern occurrence (count) | Unix Linux Forums | UNIX for Advanced & Expert Users

  Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Unix: list out Pattern occurrence (count)

UNIX for Advanced & Expert Users


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-10-2011
tp2115 tp2115 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 29 September 2011, 1:42 AM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Unix: list out Pattern occurrence (count)

Need to search a pattern occurrence (count) in a specified file.
Below is the details


$ cat fruits
apple apple
ball ball
apple
ball ball ball
apple apple apple
cat cat
cat cat cat
apple
apple
Note: If I'll use the grep command with -c option then it'll count the 1st occurrence in the lines but not the repeated occurrence like

$ grep -ic apple fruits
5
but here apple is occurring 8 times.
Sponsored Links
    #2  
Old 08-10-2011
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 974 Times in 790 Posts
Perhaps you could translate all whitespace characters to newlines before piping into grep.

Regards,
Alister
Sponsored Links
    #3  
Old 08-10-2011
tp2115 tp2115 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 29 September 2011, 1:42 AM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Could you please give me an example
    #4  
Old 08-10-2011
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 12 September 2014, 1:32 PM EDT
Posts: 3,710
Thanks: 7
Thanked 1,142 Times in 1,113 Posts

Code:
cat file | tr " " "\n"

Sponsored Links
    #5  
Old 08-10-2011
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 18 September 2014, 5:28 PM EDT
Location: Saskatchewan
Posts: 19,372
Thanks: 788
Thanked 3,266 Times in 3,061 Posts

Code:
tr " " "\n" < file

Sponsored Links
    #6  
Old 08-10-2011
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 8 August 2014, 4:56 AM EDT
Posts: 3,057
Thanks: 33
Thanked 658 Times in 637 Posts

Code:
 
$ nawk '{for(i=1;i<=NF;i++)if($i == "apple") ++count;} END{print count}' test
8
$ cat test
apple apple
ball ball
apple
ball ball ball
apple apple apple
cat cat
cat cat cat
apple
apple

Sponsored Links
    #7  
Old 08-10-2011
yazu yazu is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 25 May 2014, 1:25 AM EDT
Location: From far
Posts: 987
Thanks: 21
Thanked 232 Times in 226 Posts

Code:
perl5.10.1 -0777 -nE 'say scalar grep {/\bapple\b/} split' INPUTFILE
8

I love perl. :-)
Sponsored Links
Closed 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
Select everything between first and last occurrence of same pattern usha rao Shell Programming and Scripting 25 06-22-2011 07:40 AM
Count no of occurrence of the strings based on column value zooby Shell Programming and Scripting 8 04-26-2011 10:02 AM
extract first occurrence in a list viralnerd UNIX for Dummies Questions & Answers 5 12-16-2009 05:50 AM
Count the number of occurrences of a pattern between each occurrence of a different pattern slipstream Shell Programming and Scripting 3 11-10-2009 03:06 AM
count a occurrence soemac Shell Programming and Scripting 5 12-15-2008 05:10 AM



All times are GMT -4. The time now is 05:44 PM.