Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 12-01-2009, 07:03 AM   #1
Registered: Aug 2009
Posts: 539

Rep: Reputation: 33
How to get the count by using grep?

I want to find string in a log file by using:
grep mytsring mylog.log
It list quite a lot of found strings. But I want to know how many instead of counting it manually.

Old 12-01-2009, 07:05 AM   #2
Senior Member
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
grep mystring mylog.log | wc -l
That counts the number of lines in the output (wc = word count, -l = lines).
Old 12-02-2009, 01:41 AM   #3
LQ Newbie
Registered: Mar 2008
Location: NJ
Distribution: Suse
Posts: 18

Rep: Reputation: 3
I've found the man pages to quite helpful.

from the man page for grep:
man grep
General Output Control
-c, --count
Suppress normal output; instead print a count of matching lines for each input file. With the -v, --invert-match option (see below),
count non-matching lines. (-c is specified by POSIX.)

grep mytsring mylog.log
returns many results and

grep -c mystring mylog.log
will return an integer that indicates the # of hits grep finds.

Old 12-02-2009, 02:40 AM   #4
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
only grep -c will give the number of lines that have the search string.
In case you have the string repeat on a line and want to count them too then try the following

grep -o mystring mylog.log | grep -c mystring


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
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
Need a way to count sub-directories and get a total count Mo-regard Linux - Newbie 1 08-14-2009 09:10 AM
How to grep and count on a single go? ZAMO Linux - Software 4 01-15-2009 03:08 AM
Word count with grep DiagonalArg Linux - Software 3 02-13-2006 12:46 PM
Should posts in general count on your post count? Joey.Dale General 16 01-27-2004 01:31 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:59 AM.

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