egrep - Help Required

HI,
I have a piece of code which checks the return value for a pattern "Status: 0000" like this -

returnval=<<some op>>
 
echo $returnval | egrep "\bStatus:\s[0]+"
if [ $? -ne 0]; then
  echo "Failed"
fi

Even on success or failure i get $? = 1 i.e. it never passes ?

For me the \s is breaking it.

echo $returnval | egrep "\bStatus: *[0]+"

Hi,
I tried, still returns 1.:confused:

as is the 0] typo...

Can you please elaborate ? Did not get you?

returnval=<<some op>>
 
echo $returnval | egrep "\bStatus:\s[0]+"
if [ $? -ne 0]; then
  echo "Failed"
fi

should be:

returnval=<<some op>>
 
echo $returnval | egrep "\bStatus:\s[0]+"
if [ $? -ne 0 ]; then
  echo "Failed"
fi

No. That is fine. I double checked. Might be typo was while typing here :slight_smile:

curleb was referring to:

if [ $? -ne 0]; then

which should be:

if [ $? -ne 0 ]; then

That your egrep always returns 1 indicates that you if statement if actually fine, but that your egrep is not finding anything.

I could find no reference to \s in the manual, although I presume it to mean whitespace (or space)...

If that piece of code is in a script; try to rrun the commands interactively. I found some differences in the behaviour of egrep (up to a total freeze !) while running interactively or in a script.

try out...
$ echo "Status: 0000" | egrep "\bStatus: *([0]+)";
Status: 0000

gives me the correct output..as proposed by scott above....

Hi,
It is giving me variable syntax error and if type directly on the Unix Window, does not show any output

On Solaris 10:

-> echo "Status: 0000" | egrep "\bStatus: *([0]+)" && echo $? || echo $?
1

-> echo "Status: 0000" | egrep "Status: *([0]+)" && echo $? || echo $?  
Status: 0000
0

Hi,
I have Solaris 8 Sun Blade Ultra 150 system. And, I tried your command in the Unix Window. Its still showing "Variable Syntax". :confused: Thanks for your support though. Please help how do I get over this error.

Basically, I get huge data as a return value. In that data this "Status" is present. hence, I want to grep on this status and update the user accordingly.

Okay:

1) What shell are you using? ksh?

2) What exactly do you mean by 'huge data as a return value'? Do you have a terminal output that you can post, either for the problem we're wading through here...or the original 'huge data' that you're trying to resolve...?

Hi,
I guess Regular expressions are just not working. I saved the out out in a temp file and tried this for drilling down the issue :-

This works now. Thanks to curleb for pointing out the regex

egrep "Status: *([0]+)" without a "\b"
egrep "Status: *([0]+)" tempFile.txt
result=$?
if [ $result -ne 0 ]; then
   echo "Failed"
else
   echo "Succeeded"
fi

But, the other way i.e. taking using a variable instead of a file does not work.

Any idea ?

I'm to hedge and say you've only resolved an immediate concern...you might not have resolved your real concern. You never responded on the 'huge data' question, so I'm guessing you might still see the issue in another spot. In fact, your sample if..then statement appears to be a mock-up instead of a real-life condition. What is it you're trying to accomplish? Are you trying to capture SQL return codes or something...?

You're only going to see a variable work if you've piped something into it; $? is the same concept, but on the shell's level instead of the process. Whatever allows you to redirect into tempfile.txt should also allow you to simply pipe this same result into your egrep statement, and thereby get $return to match your goal.

HTH

Hi,
It is a network response over TCP/IP channel. So, if the transfer of data fails, I get some response. In that response, there are Status codes. So, to check for success, I need to grep on that status code and then show an error dialog to the user.

So, piping variables was not working. Hence, I decided to dump the response in a file and then do the egrep. That worked !