LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 02: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, 02:41 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Code:
STATUS1=$(grep -c "error_phrases" /home/psbatch/batch/error.log)
echo "STATUS1 (error phrases grep, 0=good): $STATUS1"
 
Old 01-26-2012, 03: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, 12: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, 01:01 AM   #5
devUnix
Member
 
Registered: Oct 2010
Posts: 606

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



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 01:06 PM
grep not working gdanko Linux - General 11 07-15-2010 02:28 PM
grep *.tpl is not working ravipat Linux - Newbie 3 07-03-2008 08:18 PM

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

All times are GMT -5. The time now is 05:08 PM.

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