Operating System - HP-UX
1753649 Members
6012 Online
108798 Solutions
New Discussion юеВ

Re: grep working differently

 
MAYIANAN
Advisor

grep working differently

Hi,

I am trying to connect oracle and get a date by using

YESDATE=`echo "select to_char(to_date('20100130', 'YYYYMMDD') - 1, 'YYYYMMDD') from dual;" | sqlplus -s ${CONNECT_STR} | grep [0-9]`

In my home directory it returns 20100129

but in our main directory it returns null since it tries to grep like 'grep 0 1'

Wonder why it grep like this instead of [0-9]

please help me out.

-Mayil
4 REPLIES 4
Steven Schweda
Honored Contributor

Re: grep working differently

> please help me out.

You first.

> but in our main directory it returns null
> since it tries to grep like 'grep 0 1'

How did you decide this? What do you get if
you leave off the "grep [...]"? That is,
what gets fed into the "grep" command?
MAYIANAN
Advisor

Re: grep working differently

In the statement i have hardcoded the date, means some number is passed to the grep but still it returns null.

I tried this in command line
"echo "12string"|grep [0-9]"

It returns 12string in my home directory
but nothing when i do the same in main directory.
smatador
Honored Contributor

Re: grep working differently

Hi,
What is for you the MAIN directory? If I were you I will check for environment variables to troubleshoot
# env in main and home directory
# whereis grep or which grep
After that get the location path of grep and test another time
# /usr/bin/grep
HTH
Dennis Handly
Acclaimed Contributor

Re: grep working differently

>YESDATE=`echo "select to_char(to_date('20100130', 'YYYYMMDD') - 1, 'YYYYMMDD') from dual;" | sqlplus -s ${CONNECT_STR} | grep [0-9]`
>in our main directory it returns null since it tries to grep like 'grep 0 1'
>Wonder why it grep like this instead of [0-9]

You haven't quoted it correctly, nor used $():
YESDATE=$(echo "select to_char(to_date('20100130', 'YYYYMMDD') - 1, 'YYYYMMDD') from dual;" | sqlplus -s ${CONNECT_STR} | grep "[0-9]")

In particular do the following in both directories:
echo "[0-9]"
echo [0-9]

>It returns 12string in my home directory but nothing when I do the same in main directory.

If you use tusc, you can see exactly what grep sees. And how the shell stabs you in the back. :-)

>Steven: >>grep like 'grep 0 1'
>How did you decide this?

That would be good to know. Either Mayil used tusc or Mayil is very clever on analyzing and providing test output.

In any case, it tells me a possible solution.