grep function | Unix Linux Forums | UNIX Desktop for Dummies Questions & Answers

  Go Back    


UNIX Desktop for Dummies Questions & Answers Discuss UNIX and Linux user interfaces like GNOME, KDE, CDE, and Open Office here. All UNIX and Linux Newbies Welcome !!

grep function

UNIX Desktop for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-09-2011
vandi vandi is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 9 February 2011, 11:30 PM EST
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
grep function

Hi Guys,

I have a very limited knowledge on shell scripting.
When I execute dspmq, I get either

" Running" or "Running in Standby" as output

HTML Code:
$dspmq
QM1    Running
QM2    Running as StandBy
I want my script to run only if the output of dspmq is "Running".

I executed the below command
HTML Code:
dspmq -m qmgrname |grep "Running
"

But my script is getting kicked off for both "Running" and "Running as Standby"

When I grep for "Running", I am getting both "Running" and "Running as Standby".

As "Running" is available in both the strings, I am getting my script kicked for both cases.
How do I get my script kicked for "Running" alone.

Let me know your thoughts.
Sponsored Links
    #2  
Old 02-10-2011
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 23 October 2014, 5:35 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,466
Thanks: 8
Thanked 548 Times in 525 Posts
Grep uses regex, $ is meta for end of line, and beware trailing white space.
Code:
 
grep 'Running *$'

Line tools like grep do not work for linefeeds in the pattern, as the buffer is one line.
Sponsored Links
    #3  
Old 02-25-2011
dinas009 dinas009 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 25 February 2011, 8:43 AM EST
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
error

Last edited by dinas009; 02-25-2011 at 08:37 AM..
    #4  
Old 02-25-2011
wempy's Avatar
wempy wempy is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 18 July 2013, 6:39 AM EDT
Location: Harpenden, UK
Posts: 219
Thanks: 0
Thanked 3 Times in 3 Posts
why don't you grep for "as StandBy" i.e.

Code:
dspmq |grep "as StandBy" && {
echo "kick off your script here within these braces"
}

Sponsored Links
    #5  
Old 03-01-2011
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 29 July 2014, 8:57 AM EDT
Posts: 98
Thanks: 0
Thanked 10 Times in 10 Posts
Assuming bash/ksh/zsh:
Code:
if [ $(dspmq |grep -vc "as StandBy") -ge 1 ]
then
# code here
fi

This counts the number of lines that don't match "as StandBy"


Andrew
Sponsored Links
    #6  
Old 03-01-2011
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 23 October 2014, 5:35 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,466
Thanks: 8
Thanked 548 Times in 525 Posts
For grep, the nicest test wrapper is the -q, as it is terse, grep can stop at the first hit and just exit 0 else exit 1 at eof, with no additional interpreting commands like -ge:

Code:
if ( dspmq | grep -q "as StandBy" )
then
 . . .
else
 . . .
fi

I guess even the () can be dropped, as they cost you a fork for a subshell, but I like some wrapper.
Sponsored Links
    #7  
Old 03-01-2011
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 29 July 2014, 8:57 AM EDT
Posts: 98
Thanks: 0
Thanked 10 Times in 10 Posts
Quote:
Originally Posted by DGPickett View Post
For grep, the nicest test wrapper is the -q, as it is terse, grep can stop at the first hit and just exit 0 else exit 1 at eof, with no additional interpreting commands like -ge:

Code:
if ( dspmq | grep -q "as StandBy" )

OP is interested in "Running", not "Running as Standby". Your solution would yield a false positive. Yes, using
Code:
if ( dspmq | grep -q -v "as StandBy" )

will work without giving a false positive, but certain Unices don't follow the POSIX switch for "quiet", using -s instead.

Andrew
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
Perl - Grep function regular expression adelsin Shell Programming and Scripting 4 08-19-2010 12:08 PM
Help in grep function or similar using awk Lucky Ali Shell Programming and Scripting 1 10-20-2009 03:36 PM
Return a value from called function to the calling function mvictorvijayan Shell Programming and Scripting 1 09-14-2009 04:19 AM
Help with Grep Function msbaath UNIX for Dummies Questions & Answers 3 04-22-2009 10:36 AM
Passing global variable to a function which is called by another function sars Shell Programming and Scripting 4 06-30-2008 11:39 AM



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