Is there a command line option on grep that says count ALL occurances on a line not just the first one???
echo "jerry jerry" | grep -c jerry
only returns 1 and not 2. Looking at the man page I did not see anything like that.
Thanks,
Jerry
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10/10/07, Jerry Geis wrote:
Is there a command line option on grep that says count ALL occurances on a line not just the first one???
echo "jerry jerry" | grep -c jerry
Grep can just count the number of lines with occurrences not the number of them. You can do it in perl though.
# echo "jerry jerry" | perl -lane '$count = grep $_ =~ m/jerry/, @F; print $count' 2
- -- Andy Harrison public key: 0x67518262
A simple one # echo "jerry jerry" | tr " " "\n" | grep -c jerry 2
The perl seems more general.
On 10/10/07, Andy Harrison aharrison@gmail.com wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10/10/07, Jerry Geis wrote:
Is there a command line option on grep that says count ALL occurances on a line not just the first one???
echo "jerry jerry" | grep -c jerry
Grep can just count the number of lines with occurrences not the number of them. You can do it in perl though.
# echo "jerry jerry" | perl -lane '$count = grep $_ =~ m/jerry/, @F; print $count' 2
Andy Harrison public key: 0x67518262 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) Comment: http://firegpg.tuxfamily.org
iD8DBQFHDSMVNTm8fWdRgmIRAsDIAJ9KiT+6vuaWjwezW+nd5YuHVRCJ6wCfQIoq Wat5Wo5Kmsup6XnJ06RVaVo= =WlD0 -----END PGP SIGNATURE----- _______________________________________________ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Stephen Harris wrote:
On Wed, Oct 10, 2007 at 04:32:30PM -0300, mups.cp wrote:
A simple one # echo "jerry jerry" | tr " " "\n" | grep -c jerry 2
But what about jerry.jerry or jerry/jerry or jerry,jerry or....
Well he never stated a non-standard field separator, but if that is the case, with awk:
echo "jerry,jerry-jerry jerry" | awk 'BEGIN{RS="[^[:alnum:]]"} /^jerry$/ {jerry++} END{print jerry}'
That'll separate records by any non-alphanumeric.
(I had messed up the comparison on my first post)
-Ross
______________________________________________________________________ This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender and permanently delete the original and any copy or printout thereof.
On Wed, Oct 10, 2007 at 04:10:05PM -0400, Ross S. W. Walker wrote:
Stephen Harris wrote:
On Wed, Oct 10, 2007 at 04:32:30PM -0300, mups.cp wrote:
A simple one # echo "jerry jerry" | tr " " "\n" | grep -c jerry 2
But what about jerry.jerry or jerry/jerry or jerry,jerry or....
Well he never stated a non-standard field separator, but if that is the case, with awk:
The question wasn't suficiently well defined. After all, is "jerryjerry" a count of "1" or "2" ? :-) :-)
To make the "tr" case more general: echo "jerry jerry" | tr -c '[:alnum:]' '\012' | grep -c jerry
Or we could be silly (if each occurance is meant to be counted): echo "jerry jerryjerryjerryhellojerry" | sed 's/jerry/\ jerry/g' | grep -c jerry
Stephen Harris wrote:
On Wed, Oct 10, 2007 at 04:10:05PM -0400, Ross S. W. Walker wrote:
Stephen Harris wrote:
On Wed, Oct 10, 2007 at 04:32:30PM -0300, mups.cp wrote:
A simple one # echo "jerry jerry" | tr " " "\n" | grep -c jerry 2
But what about jerry.jerry or jerry/jerry or jerry,jerry or....
Well he never stated a non-standard field separator, but if
that is the case, with awk:
The question wasn't suficiently well defined. After all, is "jerryjerry" a count of "1" or "2" ? :-) :-)
To make the "tr" case more general: echo "jerry jerry" | tr -c '[:alnum:]' '\012' | grep -c jerry
Or we could be silly (if each occurance is meant to be counted): echo "jerry jerryjerryjerryhellojerry" | sed 's/jerry/\ jerry/g' | grep -c jerry
True!
Regular expressions sure are fun :-)
I think we've shown enough variations here to give the OP the idea that there are a lot of ways to skin this cat.
-Ross
______________________________________________________________________ This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender and permanently delete the original and any copy or printout thereof.
On 10/10/07, Ross S. W. Walker rwalker@medallion.com wrote: <snip>
True!
Regular expressions sure are fun :-)
I think we've shown enough variations here to give the OP the idea that there are a lot of ways to skin this cat.
But we have to think him for the mental stimulation. Its nice to see the occasional simple question (as opposed to, "how do I flash my bios from linux" type questions).
...james
On Wed, 2007-10-10 at 16:10 -0400, Ross S. W. Walker wrote:
Stephen Harris wrote:
On Wed, Oct 10, 2007 at 04:32:30PM -0300, mups.cp wrote:
<snip>
Well he never stated a non-standard field separator, but if that is the case, with awk:
echo "jerry,jerry-jerry jerry" | awk 'BEGIN{RS="[^[:alnum:]]"} /^jerry$/ {jerry++} END{print jerry}'
That'll separate records by any non-alphanumeric.
(I had messed up the comparison on my first post)
I just wanted to say thanks to Ross for reminding folks on the list about the existence of the venerable (g)awk.
I think a lot of folks would be pleasantly surprised about its power and simplicity of use, if one is already regex familiar.
Personally, in many years of use, I feel I still do not need to learn Perl (although I did get a book and dabble with it some) for 99.9% of things that *I* do.
Awk does the job for me.
-Ross
<snip sig stuff>
-- Bill
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10/11/07, William L. Maltby wrote:
Personally, in many years of use, I feel I still do not need to learn Perl (although I did get a book and dabble with it some) for 99.9% of things that *I* do.
Awk does the job for me.
If it's any consolation, my little recipe was using perl in awk mode (-a), as it's occasionally referred to as... :)
- -- Andy Harrison public key: 0x67518262
William L. Maltby wrote:
On Wed, 2007-10-10 at 16:10 -0400, Ross S. W. Walker wrote:
Stephen Harris wrote:
On Wed, Oct 10, 2007 at 04:32:30PM -0300, mups.cp wrote:
<snip>
Well he never stated a non-standard field separator, but if
that is the case, with awk:
echo "jerry,jerry-jerry jerry" | awk
'BEGIN{RS="[^[:alnum:]]"} /^jerry$/ {jerry++} END{print jerry}'
That'll separate records by any non-alphanumeric.
(I had messed up the comparison on my first post)
I just wanted to say thanks to Ross for reminding folks on the list about the existence of the venerable (g)awk.
I think a lot of folks would be pleasantly surprised about its power and simplicity of use, if one is already regex familiar.
Personally, in many years of use, I feel I still do not need to learn Perl (although I did get a book and dabble with it some) for 99.9% of things that *I* do.
Awk does the job for me.
Well thank you, but we shouldn't forget about sed either, it is even smaller and can be just as powerful too (check out some of the advanced sed scripts floating around the net).
I like perl for the advanced scripting applications it can create, but like python and other rich scripting languages sometimes it is just a little thick for what needs to be done.
With such an extensive tool box there is always just the right tool for the job.
-Ross
______________________________________________________________________ This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender and permanently delete the original and any copy or printout thereof.
mups.cp wrote:
A simple one # echo "jerry jerry" | tr " " "\n" | grep -c jerry 2
The perl seems more general.
To add yet another variation:
echo "jerry jerry" | awk 'BEGIN {RS=FS} {/jerry/ jerry++} END {print jerry}'
-Ross
On 10/10/07, Andy Harrison aharrison@gmail.com wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10/10/07, Jerry Geis wrote:
Is there a command line option on grep that says count ALL occurances on a line not just the first one???
echo "jerry jerry" | grep -c jerry
Grep can just count the number of lines with occurrences not the number of them. You can do it in perl though.
# echo "jerry jerry" | perl -lane '$count = grep $_ =~ m/jerry/, @F; print $count' 2
Andy Harrison public key: 0x67518262 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) Comment: http://firegpg.tuxfamily.org
iD8DBQFHDSMVNTm8fWdRgmIRAsDIAJ9KiT+6vuaWjwezW+nd5YuHVRCJ6wCfQIoq Wat5Wo5Kmsup6XnJ06RVaVo= =WlD0 -----END PGP SIGNATURE----- _______________________________________________ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
______________________________________________________________________ This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender and permanently delete the original and any copy or printout thereof.
On 10/10/07, Ross S. W. Walker rwalker@medallion.com wrote:
mups.cp wrote:
A simple one # echo "jerry jerry" | tr " " "\n" | grep -c jerry 2
The perl seems more general.
To add yet another variation:
echo "jerry jerry" | awk 'BEGIN {RS=FS} {/jerry/ jerry++} END {print jerry}'
Did you mean:
echo "jerry jerry" | awk 'BEGIN {RS=FS} /jerry/ {jerry++} END {print jerry}'
The one you gave does not handle "jerry blue jerry" for instance?
Cheers...james
James Olin Oden wrote:
On 10/10/07, Ross S. W. Walker rwalker@medallion.com wrote:
mups.cp wrote:
A simple one # echo "jerry jerry" | tr " " "\n" | grep -c jerry 2
The perl seems more general.
To add yet another variation:
echo "jerry jerry" | awk 'BEGIN {RS=FS} {/jerry/ jerry++}
END {print jerry}'
Did you mean:
echo "jerry jerry" | awk 'BEGIN {RS=FS} /jerry/ {jerry++} END {print jerry}'
The one you gave does not handle "jerry blue jerry" for instance?
Yes, thanks, I realized that after hitting send...
-Ross
______________________________________________________________________ This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender and permanently delete the original and any copy or printout thereof.
Ross S. W. Walker wrote:
mups.cp wrote:
A simple one # echo "jerry jerry" | tr " " "\n" | grep -c jerry 2
The perl seems more general.
To add yet another variation:
echo "jerry jerry" | awk 'BEGIN {RS=FS} {/jerry/ jerry++} END {print jerry}'
That should have been:
echo "jerry jerry" | awk 'BEGIN{RS=FS} /^jerry$/ {jerry++} END{print jerry}'
-Ross
On 10/10/07, Andy Harrison aharrison@gmail.com wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 10/10/07, Jerry Geis wrote:
Is there a command line option on grep that says count ALL occurances on a line not just the first one???
echo "jerry jerry" | grep -c jerry
Grep can just count the number of lines with occurrences not the number of them. You can do it in perl though.
# echo "jerry jerry" | perl -lane '$count = grep $_ =~
m/jerry/, @F;
print $count' 2
Andy Harrison public key: 0x67518262 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) Comment: http://firegpg.tuxfamily.org
iD8DBQFHDSMVNTm8fWdRgmIRAsDIAJ9KiT+6vuaWjwezW+nd5YuHVRCJ6wCfQIoq Wat5Wo5Kmsup6XnJ06RVaVo= =WlD0 -----END PGP SIGNATURE----- _______________________________________________ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender and permanently delete the original and any copy or printout thereof.
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
______________________________________________________________________ This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender and permanently delete the original and any copy or printout thereof.