LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to grep and count on a single go? (https://www.linuxquestions.org/questions/linux-software-2/how-to-grep-and-count-on-a-single-go-697427/)

ZAMO 01-15-2009 02:38 AM

How to grep and count on a single go?
 
Hi,
Is it possible to do a grep and do the count for number of occurrence?

Say, am going for this

Code:

grep zamo */*/*
grep zamo */*/* |wc -l

How to make it a single line ? Another Question pop here, in case if i get a output error, like permission denied, how to ignore that during count.

Thanks

indeliblestamp 01-15-2009 02:46 AM

If you use grep -c, you'll get a count for each file, and then you'd have to add it. I guess doing a grep first and then piping it to wc is the easiest way. You can redirect stderr to /dev/null to skip error messages:
Code:

grep zamo */*/* 2>/dev/null | wc -l
(I just tried this, and wc cleverly counts only the lines from stdout. So your count will remain the same whether or not you suppress the error messages).

chakka.lokesh 01-15-2009 02:47 AM

check the -c option

ZAMO 01-15-2009 02:50 AM

Thanks Arun, I forgot the -c switch . Further do "2>/dev/null " can supress any error to print on screen? Is it?

indeliblestamp 01-15-2009 03:08 AM

Yes, that command prevents errors from appearing on the screen by redirecting them to /dev/null. You could redirect it to a file for later reading too, if you use something like "2>/tmp/grep-errors.txt". Either way it won't appear on screen.


All times are GMT -5. The time now is 07:21 AM.