- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Count the number of occurances of a word
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-21-2009 04:26 AM
тАО01-21-2009 04:26 AM
I would like to count the number of occurances of 'foo' in a file.
If I grep for 'foo' I can wc to get the number of lines but some lines may have more than one occurance of 'foo'.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-21-2009 04:42 AM
тАО01-21-2009 04:42 AM
SolutionSomething like:
perl -ne '$hits += s/\bfoo\b/XXX/g }{ print qq($hits hits.\n)'
If you need to to this for many strings, then you can use shell substitutions, $ENV{} array entries or 'shift' to get the string values which is hard-coded as 'foo' in this example.
Change /g to /gi for case-insensitive matches.
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-21-2009 06:23 AM
тАО01-21-2009 06:23 AM
Re: Count the number of occurances of a word
it is important to know, whether really a word or a string count is wanted.
Besides this,
grep -c foo filename
is a far better line-counter than
grep foo
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-21-2009 06:27 AM
тАО01-21-2009 06:27 AM
Re: Count the number of occurances of a word
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-21-2009 02:25 PM
тАО01-21-2009 02:25 PM
Re: Count the number of occurances of a word
If you have to have this correct, you can grep -w for foo, then use tr to break up the output, then count the lines with foo:
grep -w foo | tr -s " " "\012" | grep -c foo
Note this assumes space delimited. If you have more punctuation chars, you would have to add that to tr(1).