Grep command with multiple pattern

Hi,

I want to search multiple patterns in a variable.

DB_ERR=`echo "$DB_TRANS" | grep "SP2-" | grep "ORA-"`
echo $DB_ERR

But I am not getting anything in DB_ERR.
I want to print each line on seperate line. Could you please help me out in this. Thanks in advance.

use egrep like so:

egrep '(this|that)'

using this,

DB_ERR=`echo "$DB_TRANS" | egrep `(SP2- | ORA-)``

I am getting error as,

0403-057 Syntax error at line 50 : `(' is not expected.

So I removed brackets, but still not getting expected output.
Please help me out.

The backticks in red are supposed to be single quotes:

DB_ERR=`echo "$DB_TRANS" | egrep `(SP2- | ORA-)``

Tried with single quotes (' '), but still no output.

You could try:

DB_ERR=$(echo "$DB_TRANS" | grep -E "SP2- | ORA-")

Still I am not getting what i expect.
If I provide wrong data to get what i want and printed,

echo $DB_TRANS

output of it is,

SQL*Plus: Release 9.2.0.5.0 - Production on Fri Oct 8 13:33:15 2010 Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved. 
SP2-0306: Invalid option. Usage: CONN[ECT] [logon] [AS {SYSDBA|SYSOPER}] where <logon> ::= <username>[/<password>]
[@<connect_string>] | / Enter user-name: SP2-0306: Invalid option. Usage: CONN[ECT] [logon] [AS {SYSDBA|SYSOPER}] 
where <logon> ::= <username>[/<password>][@<connect_string>] | / Enter user-name: SP2-0306: Invalid option. 
Usage: CONN[ECT] [logon] [AS {SYSDBA|SYSOPER}] where <logon> ::= <username>[/<password>][@<connect_string>] |
 / SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus

but expected output should be,

SP2-0306: Invalid option. Usage: CONN[ECT] [logon] [AS {SYSDBA|SYSOPER}] 
where <logon> ::= <username>[/<password>][@<connect_string>] | / Enter user-name: SP2-0306: Invalid option. 
Usage: CONN[ECT] [logon] [AS {SYSDBA|SYSOPER}] where <logon> ::= <username>[/<password>]
[@<connect_string>] | / SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus

I want a code for ksh script which prints output in each line . Thanks in advance.

man grep, since you have not (or Im half blind today...) said what OS.
For instance, in HP-UX 11 and previous you have no option -w.
to have something alike you can do:

# grep -e "^abc" -e " abc " -e "abc$" filename

Hi,

You can try below,

DB_ERR=`echo "$DB_TRANS" | egrep "[SP2-,ORA-]"`

Thanks vbe. Now I am getting all errors started from SP2 and ORA.
My code is,

DB_ERR=$(echo "$DB_TRANS" | grep -e "SP2-" -e "ORA-")
echo $DB_ERR

But I am getting all erros in one line I need each line to be printed on seperate line.
Help me out.

.... | egrep -e 'SP2-|ORA-'

The problem appears to be that $DB_TRANS is an environment variable not a pointer to the name of a file. If this is the case the line break between messages is lost and you just have one long string.
Needs a re-think about where to store the error messages so that they can be processed line-by-line with "egrep".

No, DB_TRANS is not an environment variable, Its,

DB_TRANS=$(sqlplus $USERID/$PASSWORD@$DATABASE << EOF 
set head off 
set feed off 
set serveroutput on
CREATE TABLE TBL1(
   OLD_ID VARCHAR2(6) PRIMARY KEY,
   NEW_ID VARCHAR2(6),
   DATE  DATE
);
commit;
/ 
EOF)

From it, I am picking up the errors as,

echo $DB_TRANS
DB_ERR=$(echo "$DB_TRANS" | grep -e "SP2-" -e "ORA-")
echo $DB_ERR

So want each error on new line. Please help me out.

Sorry, but DB_TRANS is an environment variable. This is the problem. You cannot store multiple lines in an environment variable for your purpose because the line breaks get lost. The whole contents of ${DB_TRANS} is one concatonated line.

Try redirecting the output to a file not to an environment variable. You can then use "egrep" on the file as described in many earlier posts.

Thanks for reply :slight_smile:

my code,

DB_ERR=$(echo "$DB_TRANS" | grep -e "SP2-")

Using single pattern search I am able to get each error on seperate line. So asked the same. Why it is not working for multiple pattern search in grep command.