![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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? |
|
||||
|
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.) |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|