Operating System - Linux
1752693 Members
5636 Online
108789 Solutions
New Discussion юеВ

Re: egrep with variable on input

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

egrep with variable on input

Hi

This is not correct
if [[ $(echo $TNSSTRING | egrep -i (ORA-01|ORA-27) | wc -l) -gt 0 ]]; then


How to use the $TNSSTRING variable as a file for egrep ?

Bests regards
Den
14 REPLIES 14
Steven Schweda
Honored Contributor

Re: egrep with variable on input

> How to use the $TNSSTRING variable as a
> file for egrep ?

What does this mean? Does TNSSTRING contain
a file name, or does it contain the data
which you are trying to search?

One line out of a bigger script does not tell
me as much as you might hope that it does.

What does
echo $TNSSTRING
give you?

Where are the data which you are trying to
search?

Better still: What are you trying to do?

It may make more sense to find a better
solution to the actual problem than it does
to try to fix some sub-ideal scheme. But,
because my psychic powers are so weak, this
would require you to describe the actual
problem.
Leo The Cat
Regular Advisor

Re: egrep with variable on input

$TNSSTRING contains more than one lines !

Here an Example of result with $TNSSTRING = SQL*Plus: Release 10.2.0.1.0 - Production on Wed Sep 9 11:24:10 2009 Copyright (c) 1982, 2005, Oracle. All rights reserved. ERROR: ORA-12154: TNS:could not resolve the connect identifier specified SP2
-0751: Unable to connect to Oracle. Exiting SQL*Plus

The errors come because we have a ( character in the input data ...

oas_check_sqlnet.ksh[81]: 0403-057 Syntax error at line 1 : `(' is not expected.

Bests Regards
Den
Leo The Cat
Regular Advisor

Re: egrep with variable on input

Of course I could create a file first in /tmp and use it as input after but it's not very nice !
...
macosta
Trusted Contributor

Re: egrep with variable on input

I would remove the parens around the egrep pattern. Try writing it as:
egrep -i 'ORA-01|ORA-27'

You'll probably also want to put double-quotes around "$TNSSTRING" when you use it, since there's a line break and spaces in it - never assume your input will look the same every time.

Another thing, is instead of invoking "wc" to count, try using the "-q" option to egrep, then check the return code with the "$?" variable or using || and/or && operators to handle the various cases.

For example:
--
$ t="this is a string"
$ echo $t | egrep -iq 'STRING'
$ echo $?
0
$ echo $t | egrep -iq 'STRONG'
$ echo $?
1
$ echo $t | egrep -iq 'STRING' && echo Match
Match
$ echo $t | egrep -iq 'STRONG' || echo No match
No match
--

Any POSIX shell should handle this, such as ksh and bash.
Leo The Cat
Regular Advisor

Re: egrep with variable on input

Hi Macosta

The problem is not with egrep -i (ORA-01|ORA-27)

BUT

With "(" inside variables ;-)

Try your example with t="this is () a problem" and this will break your idea.

!!!

Bests Regards
Den

Steven Schweda
Honored Contributor

Re: egrep with variable on input

> With "(" inside variables ;-)

Perhaps you could provide some simple test
case which would show us exactly what you
think this problem is. I know of no
particular shell problem with "(" (or ")")
in a variable's value. For example:


bash$ t='abc
> a(b)c
> xyz'
bash$ echo "$t"
abc
a(b)c
xyz
bash$ echo "$t" | grep b
abc
a(b)c

I see what I expect.

You may have a problem somewhere in your
(complex) procedure which does some Oracle
magic, but I don't think that you know what
the problem is, or where it is. And it's
easier to see these things when you look at
smaller bits.

> The errors come because [...]

Who is complaining? What did he see? Who
sent him that stuff?
macosta
Trusted Contributor

Re: egrep with variable on input

Leo, please at try my suggestions before disagreeing with them. If you had done so, we'd be done here.

Quote your variables and replace the un-escaped parens in the egrep statement.

--
$ t="()()()()()()(())((()))"
$ echo $"t" | egrep -i (nope|nothere)
bash: syntax error near unexpected token `('
$ echo $"t" | egrep -i 'nope|nothere'
$ echo "$t" | egrep -i 'nope|()'
()()()()()()(())((()))
--

The shell is trying to interpret the parens before it even tries to run the egrep command. It's not the variable contents.

Roland Piette
Regular Advisor

Re: egrep with variable on input

Hello

(ORA-01|ORA-27) expression contains '|' wich is piping character. So when the shell interpret this string the ( char is not balanced!

I don't understand the () !
If you try to use | as or bitwise operator between two strings your syntax needs to be egrep -i "ORA-01|ORA-27"

Regards,
Roland
James R. Ferguson
Acclaimed Contributor
Solution

Re: egrep with variable on input

Hi Den:

You need to double quote the extended regular expression so that the shell doesn't interpret the special characters but lets them pass along to 'grep'.

This works (for example):

# TNSSTRING="curly {} stuff"
# [[ $(echo $TNSSTRING | egrep -i "(ORA-01|ORA-27|\{\}|())" | wc -l) -gt 0 ]] && echo OK

...as does this:

# TNSSTRING="this is () NOT a problem"

NOW, you mentioned that your TNSSTRING Variable actually consisted of multiple lines. Filters like 'grep' are not going to help here since they are line-oriented.

With Perl, you could slurp (read-up a whole file into memory) and do things like:

# TNSSTRING="Leo\nthe\ncat"
# echo ${TNSSTRING}
Leo
the
cat

# echo $TNSSTRING | perl -00 -nle 'print "MATCHED" if m{Leo\nthe\ncat}'
MATCHED

LASTLY:

While you ask interesting questions, you sometimes fail to come back to assign points to answers that have been offered. Points are not only a way of saying "thanks!" but bread-crumbs for future trollers to find the tastiest (most applicable) solution:

http://forums.itrc.hp.com/service/forums/helptips.do?#28

Please review your previous posts with unevaluated replies:

http://forums.itrc.hp.com/service/forums/pageList.do?userId=BR967434&listType=unassigned&forumId=1

The time _YOU_ take will be ample reward for the time _WE_ take to help you. Thanks.

Regards!

...JRF...