The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find files which has more than one occurance of pattern Prahlad Shell Programming and Scripting 10 08-08-2008 01:16 PM
HELP! PERL script to find matched pattern kimhuat Shell Programming and Scripting 1 05-12-2008 11:24 AM
count string occurance in a file hourly ayhanne UNIX for Dummies Questions & Answers 2 10-13-2007 11:47 AM
SED: match pattern & delete matched lines not4google Shell Programming and Scripting 7 11-22-2006 09:58 AM
appending with sed based on matched pattern jack1981 Shell Programming and Scripting 2 07-20-2006 07:54 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-24-2008
palash2k palash2k is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 12
Count of matched pattern occurance

In a file a pattern is occured many times randomly. Even it may appear more then once in the same line too. How i can get the number of times that pattern appeared in the file? let the file name is abc.txt and the pattern is "xyz".

I used the following code:
grep -ic "xyz" abc.txt

but it is not perfectly showing the result. if the pattern is repeated in a line two or more times it consider only once. so this code actually shows number of lines where pattern is found but not showing how many times in the entire file the pattern appeared? Shall i need a script for it? how can i overcome this situation?

Can anybody help me please?
  #2 (permalink)  
Old 04-24-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,342
You can try something like this:


Code:
awk -v var="xyz" '{for(i=1;i<=NF;i++){if($i==var){n++}}}END{print n}' file

Regards
  #3 (permalink)  
Old 04-24-2008
krishmaths krishmaths is offline
Registered User
  
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 191
One cheat method:


Code:
sed 's/xyz/xyz\n/g' abc.txt | grep -c xyz

  #4 (permalink)  
Old 04-24-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
If the pattern always consists of alphabetics, try something like


Code:
tr -c A-Za-z '
' <abc.txt | grep -c xyz

This replaces any non-alphabetics with newlines, so every word is on a separate line, and then, of course, it's okay that grep -c counts lines.

(Yes, the second non-option argument to tr is a literal line break between single quotes. If your tr understands some more readable notation, like maybe '\n' or '\012', then by all means use that instead.)
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0