If the grep command returns any result set | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

If the grep command returns any result set

Shell Programming and Scripting


Tags
grep -v, if condition

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-24-2010
gotam gotam is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 29 April 2011, 1:44 PM EDT
Posts: 40
Thanks: 1
Thanked 0 Times in 0 Posts
If the grep command returns any result set

my intension is to use a grep command inside the shell script
and if any row is returned or not..
depending on the resultset i have to code on further.

how to check this
i mean.. could anyone help me out with the if condition how to use it here !!
Sponsored Links
    #2  
Old 04-24-2010
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 21 October 2014, 7:52 AM EDT
Location: The Netherlands
Posts: 7,719
Thanks: 123
Thanked 538 Times in 508 Posts
This is a faq, just use the search engine right above and search for if grep.
Sponsored Links
    #3  
Old 04-24-2010
spider007 spider007 is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 29 December 2011, 12:59 PM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Gotam,

You can use the below code to test whether you got some rows using grep.....


Code:
 
count=`grep "search pattern" filename|wc -l`
if [ $count -ne 0 ]
then
     echo "pattern match found"
else
     echo "no match with the pattern"
fi

Hope this is what you are looking for....

Thanks
    #4  
Old 04-25-2010
DeCoTwc DeCoTwc is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 22 June 2014, 2:17 AM EDT
Location: NYC
Posts: 162
Thanks: 7
Thanked 8 Times in 8 Posts
Quote:
Originally Posted by spider007 View Post
Gotam,

You can use the below code to test whether you got some rows using grep.....


Code:
 
count=`grep "search pattern" filename|wc -l`
if [ $count -ne 0 ]
then
     echo "pattern match found"
else
     echo "no match with the pattern"
fi

Hope this is what you are looking for....

Thanks
While this would certainly work, it's more complex than it has to be. What I believe the OP is (should be) looking for are exit statuses. You should do some googling to get some more info, but I'll give you a quick rundown.

When you run a command, after it completes there is something called an exit value. Generally if a command is successful, the exit value is 0, and if it fails, the value is 1 (if you look in the man pages for a command, sometimes it may have a whole list of exit statuses for each possible scenario). You can see what a commands exit status is by looking at the variable $?.

So what you want is to check if the exit status of your grep command is a 0 or a 1:

Let's take a look below:


Code:
(03:28:07\[D@DeCoBox15)
[~]$ cat animals
cat
dog
hippo

(03:30:50\[D@DeCoBox15)
[~]$ grep dog animals
dog

(03:30:57\[D@DeCoBox15)
[~]$ echo $?
0

(03:31:00\[D@DeCoBox15)
[~]$ grep mouse animals

(03:31:07\[D@DeCoBox15)
[~]$ echo $?
1

As you can see, when the grep succeeds, the exit status was 0, and when it failed (because there is not mouse in the animals file) it was 1. So now all you have to do is build a if this than or if that....yadda yadda.

But wait...there's more. If you want to do something like this in a one liner you can use conditional checking syntax: && (if the last command succeeded) and || (if the last command failed). So again, if you want to use this with grep:


Code:
(03:31:12\[D@DeCoBox15)
[~]$ grep cat animals >/dev/null && echo "Yep, it is there" || echo "Sorry, could not find it"
Yep, it is there

(03:35:00\[D@DeCoBox15)
[~]$ grep mouse animals >/dev/null && echo "Yep, it is there" || echo "Sorry, could not find it"
Sorry, could not find it

So in this example, we're just echoing stuff based on the results...but we could be doing anything.

Hope this helps...it's amazing how much I'll type when I can't sleep.
Sponsored Links
    #5  
Old 04-25-2010
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 October 2014, 8:25 PM EDT
Location: Amsterdam
Posts: 9,538
Thanks: 284
Thanked 2,422 Times in 2,172 Posts
Or perhaps easier to understand and if grep has the -q (quiet) option instead of > /dev/null:


Code:
if grep -q "search pattern" filename
then
  echo "pattern match found"
else
  echo "no match with the pattern"
fi

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find/grep returns no matches ulrith Shell Programming and Scripting 2 02-14-2010 06:54 AM
Grep without returns... mrwatkin UNIX for Dummies Questions & Answers 1 07-24-2009 01:22 PM
How to check if a command returns nothing Alvescot Shell Programming and Scripting 2 05-21-2009 11:00 AM
Use grep result to execute next command iririr Shell Programming and Scripting 5 02-16-2009 06:36 PM
assign subst|grep|sed command result to a variable snowbiker99 Shell Programming and Scripting 5 11-14-2007 06:08 PM



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