- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- count occurrence of a regex in a very long line
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
Forums
Discussions
Discussions
Discussions
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
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
11-21-2005 09:17 AM
11-21-2005 09:17 AM
eg: quick brown fox jumps on the lazy dog quick brown fox jumps on the lazy dog quick brown fox jumps on the lazy dog quick brown fox jumps on the lazy dog quick brown fox jumps on the lazy dog quick brown fox jumps on the lazy dog quick brown fox jumps on the lazy dog quick brown fox jumps on the lazy dog quick brown fox jumps on the lazy dog
I need the count for "jumps" for instance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2005 09:25 AM
11-21-2005 09:25 AM
Re: count occurrence of a regex in a very long line
Perhaps process the string with awk?
echo $string | awk -F '{print $1 ... }'
Maybe someting with xargs, not sure.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2005 09:27 AM
11-21-2005 09:27 AM
Re: count occurrence of a regex in a very long line
Ah, forget that stuff..... in my first post, unless it unexpected helpful.
store the data in a file:
grep jumps filename | wc -l
Gets you count of jumps.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2005 09:30 AM
11-21-2005 09:30 AM
Re: count occurrence of a regex in a very long line
# awk -F"jumps" '{cnt+=(NF-1)} END{print cnt}'
regards!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2005 09:30 AM
11-21-2005 09:30 AM
Re: count occurrence of a regex in a very long line
I tried
awk -F" " '{print NF}' filename
awk: record ... too long. Any other ideas?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2005 09:45 AM
11-21-2005 09:45 AM
SolutionHaving a bad day? (this isn't like you!)
grep jumps filename | wc -l
will tell you how many lines "jumps" appears on, not how many times it appears in one line.
I can't think of an elegent way of doing this, given the regular expression requirement... UNIX shell tools are all pretty much line-based.
You could use 'sed' to change spaces to \r (ie: so that each word is on it's own line), then pipe it through grep|wc or awk... or something similar, but that'll work only if the regex you're looking for does not contain spaces.
Of course, there's always a C program - that's ALWAYS elegent!
Regards,
Vince
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2005 09:50 AM
11-21-2005 09:50 AM
Re: count occurrence of a regex in a very long line
# perl -lne '$i++ while m/jumps/g;END{print $i}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2005 09:52 AM
11-21-2005 09:52 AM
Re: count occurrence of a regex in a very long line
oh - and I also meant \n, not \r...
echo "whatever" | tr ' ' '\n' | grep jumps | wc -l
"tr" is smaller and more efficient than sed since it does so little...
-Vince
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2005 10:28 AM
11-21-2005 10:28 AM
Re: count occurrence of a regex in a very long line
I might add (of course) that you can pipe input to the perl code or specify the filename as an argument:
# perl -lne '$i++ while m/jumps/g;END{print $i}' filename
# echo ... | # perl -lne '$i++ while m/jumps/g;END{print $i}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2005 03:39 PM
11-21-2005 03:39 PM
Re: count occurrence of a regex in a very long line
An other perl way readily allowing for a regexpr:
perl -ne 'print scalar split (/jumps/)."\n"' filename
Of course this prints one too many.
So fix to:
perl -ne '$x=scalar split(/jumps/) -1; print "$x\n"' filename
Or for single count of many words on many lines:
perl -ne '$x+=scalar split(/jumps/) -1;END{print "$x\n"}' filename
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2005 04:25 PM
11-21-2005 04:25 PM
Re: count occurrence of a regex in a very long line
Are the words on line seperated by a space?? If yes, do as follows.
tr " " "\n" < your_file | grep -ic "your_reg_exp"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2005 02:38 AM
11-22-2005 02:38 AM
Re: count occurrence of a regex in a very long line
I tested all the perl and OS commands. Plain OS command run faster