Unix/Linux 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 Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 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 976 Times in 790 Posts
Perhaps you could translate all whitespace characters to newlines before piping into grep.

Regards,
Alister
Sponsored Links
    #3  
Old Unix and Linux 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 Unix and Linux 08-10-2011
bartus11's Unix or Linux Image
bartus11 bartus11 is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 3 August 2016, 11:03 AM EDT
Posts: 3,733
Thanks: 7
Thanked 1,152 Times in 1,122 Posts

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

Sponsored Links
    #5  
Old Unix and Linux 08-10-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 2 December 2016, 5:18 PM EST
Location: Saskatchewan
Posts: 21,658
Thanks: 1,008
Thanked 3,980 Times in 3,697 Posts

Code:
tr " " "\n" < file

Sponsored Links
    #6  
Old Unix and Linux 08-11-2011
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 2 December 2016, 3:48 AM EST
Posts: 3,126
Thanks: 43
Thanked 684 Times in 660 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 Unix and Linux 08-11-2011
yazu yazu is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 4 September 2016, 4:14 PM EDT
Location: From far
Posts: 1,000
Thanks: 21
Thanked 237 Times in 231 Posts

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

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



All times are GMT -4. The time now is 12:35 AM.