LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-15-2009, 03:38 AM   #1
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Rep: Reputation: 30
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
 
Old 01-15-2009, 03:46 AM   #2
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 40
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).

Last edited by indeliblestamp; 01-15-2009 at 03:47 AM. Reason: bad grammar
 
Old 01-15-2009, 03:47 AM   #3
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
check the -c option
 
Old 01-15-2009, 03:50 AM   #4
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Thanks Arun, I forgot the -c switch . Further do "2>/dev/null " can supress any error to print on screen? Is it?
 
Old 01-15-2009, 04:08 AM   #5
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 40
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
Help me in Grep Command + cd command in single line JeiPrakash Linux - Newbie 3 05-27-2008 05:16 AM
Word count with grep DiagonalArg Linux - Software 3 02-13-2006 01:46 PM
Should posts in general count on your post count? Joey.Dale General 16 01-27-2004 02:31 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration