LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 01-26-2012, 03:40 PM   #1
nishanthp82
LQ Newbie
 
Registered: Nov 2011
Posts: 15

Rep: Reputation: Disabled
grep -c not working in Linux


#!/bin/ksh
STATUS1=grep -c "error_phrases" /home/psbatch/batch/error.log
echo "STATUS1 (error phrases grep, 0=good): $STATUS1"

i try to get the count of "error_phrase" word in error.log but the shell says

./test1.ksh[2]: -c: not found [No such file or directory]

please help!
 
Old 01-26-2012, 03:41 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
Code:
STATUS1=$(grep -c "error_phrases" /home/psbatch/batch/error.log)
echo "STATUS1 (error phrases grep, 0=good): $STATUS1"
 
Old 01-26-2012, 04:17 PM   #3
nishanthp82
LQ Newbie
 
Registered: Nov 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
STATUS1=$(grep -cf $SCRDIR error_phrases)

there is a file called error_phrases but Linux is not picking up

any help?
 
Old 01-27-2012, 01:32 AM   #4
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
Which directory does the file exist in ? If it is not in your present working directory then provide the full path.
 
Old 01-27-2012, 02:01 AM   #5
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 584

Rep: Reputation: 59
Nishant:


This is not a valid stament/command:

Code:
STATUS1=grep -c "error_phrases" /home/psbatch/batch/error.log
You must change it to:

Code:
STATUS1=`grep -c "error_phrases" /home/psbatch/batch/error.log`
Notice that those are backticks and not apostrophies or single quotes.

or change it to:

Code:
STATUS1=$(grep -cf $SCRDIR error_phrases)
just waht you have posted above and it seems okay.

Now let me point out something here:

What is "$SCRDIR" doing in there? Is it the directory in which you have a log file to search for the patter in?

Note: "-f" option means you are specifying a file that contains the "pattern/keywords" you are searching for.

You would rather use:

Code:
 grep -c error_word_of_your_interest /full/path/to/your/log/file.log
First run the above command and check its result. If it is okay then move on to this step:


Code:
 COUNT=`grep -c error_word_of_your_interest /full/path/to/your/log/file.log`
or

Code:
 
COUNT=$(grep -c error_word_of_your_interest /full/path/to/your/log/file.log) 
echo "Number of Errors Found: " $COUNT

Please let us know if the above solution does not work for you. Post your exact script hre.
 
1 members found this post helpful.
  


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
grep not working right with colortail GeekyAdam Linux - Software 10 06-16-2011 02:06 PM
grep not working gdanko Linux - General 11 07-15-2010 03:28 PM
grep *.tpl is not working ravipat Linux - Newbie 3 07-03-2008 09:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:12 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration