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 Linux or Unix Question    
 
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 974 Times in 789 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 Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 28 June 2015, 6:17 PM 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: 3 July 2015, 4:40 PM EDT
Location: Saskatchewan
Posts: 20,742
Thanks: 901
Thanked 3,691 Times in 3,450 Posts

Code:
tr " " "\n" < file

Sponsored Links
    #6  
Old Unix and Linux 08-10-2011
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 25 June 2015, 3:24 AM EDT
Posts: 3,060
Thanks: 34
Thanked 661 Times in 640 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-10-2011
yazu yazu is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 22 December 2014, 12:23 AM EST
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 Linux or Unix Question

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 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 06:29 PM.