grep function


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers grep function
# 1  
Old 02-09-2011
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.
# 2  
Old 02-10-2011
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.
# 3  
Old 02-25-2011
error

Last edited by dinas009; 02-25-2011 at 09:37 AM..
# 4  
Old 02-25-2011
why don't you grep for "as StandBy" i.e.
Code:
dspmq |grep "as StandBy" && {
echo "kick off your script here within these braces"
}

# 5  
Old 03-01-2011
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
# 6  
Old 03-01-2011
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.
# 7  
Old 03-01-2011
Quote:
Originally Posted by DGPickett
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Grep -q -F , what is the function of this script

Hello, I checked grep help field, I got the answer but seems a bit technical for me. Could you please let me know what is this script doing? grep -q -F 'addprestart.sh' /usr/bin/enigma2_pre_start.sh || \ echo '/bin/sh /etc/enigma2/addprestart.sh > \ /dev/null 2>&1 & sleep .5 &' >>... (6 Replies)
Discussion started by: baris35
6 Replies

3. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

4. UNIX for Dummies Questions & Answers

GREP function in ksh which ignores LINE Breaks

Hello I am using a grep command with two patterns in my KSH script. File has line breaks in it and both the patterns are in different lines. Here is the command grep -l 'RITE AID.*ST.820' natriter820u.20140914 Pattern1 - RITE AID Pattern2 - ST*820 I am not getting any results from... (24 Replies)
Discussion started by: Raghav Garg
24 Replies

5. How to Post in the The UNIX and Linux Forums

GREP function in ksh which ignores LINE Breaks

I am using a grep command with two patterns in my KSH script. File has line breaks in it and both the patterns are in different lines. Here is the command - grep -l 'RITE AID.*ST.820' natriter820u.20140914 Pattern1 - RITE AID Pattern2 - ST*820 I am not getting any results from this,... (3 Replies)
Discussion started by: Raghav Garg
3 Replies

6. Shell Programming and Scripting

Help with grep and read function in a BASH Script

I'm putting together a script that will search my mail archives for emails that meet certain criteria and output the files to a text file. I can manually cat that text file and pipe it into sendmail and it will work (i.e. cat /pathtofile/foo.txt | sendmail -t me@company.com) My script sends... (7 Replies)
Discussion started by: binary-ninja
7 Replies

7. Shell Programming and Scripting

Cannot get grep to work within function.

Hello again, Am having an issue now with getting a simple grep command to work within a function.. The function is as below... function findRecord() { output=grep "001" recordDatabase echo $output } At the moment the "001"... (3 Replies)
Discussion started by: U_C_Dispatj
3 Replies

8. Shell Programming and Scripting

Perl - Grep function regular expression

For some reason, @logs is a list of log files @filter is a list of expressions to grep out foreach (@logs){ open READ, "<$_" or die $!; @temp=<READ>; close READ; foreach (@filter){ print grep /$_/,@temp ; } } returns a regex error in one of the files... (4 Replies)
Discussion started by: adelsin
4 Replies

9. Shell Programming and Scripting

Help in grep function or similar using awk

I have a list of id; for example: file 1 dfghd dfghe dfgey dfgeu I have another data file that contain this ids as headers; for ex. file2 >dfghd gfdgfddl;klfkld;ld;lgl;dld'l'dv >dfghe gkwhjhsgdjdjdjhjddj >dfgey jdkjfhdjhfdkjhfdkhkdk I wanted to compare file 1 and file 2... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

10. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies
Login or Register to Ask a Question