views:

105

answers:

4

The file i am working with (oraInst.loc) looks like this:

inventory_loc=/u01/app/ORAENV/oracle/oraInventory
inst_group=dba

I need to use a regular expression to grab the value between app/ and /oracle. In this case it will be ORAENV but it could be any alphanumeric string of any case and length but with no spaces.

From what I have read so far using grouping seems to be the way to do this but I just can't get my head round it.

I am using egrep on Solaris 10 as the regex engine.

+3  A: 

Try this:

\/app\/([\d\w]+)\/oracle\/

Andrew Hare
bash-3.00# egrep /app/([\d\w]+)/ /var/opt/oracle/oraInst.locbash: syntax error near unexpected token `('Is this a problem with egrep?
arthurd
I fixed it, sorry :)
Andrew Hare
bash-3.00# egrep \/app\/([\d\w]+)\/oracle\/ /var/opt/oracle/oraInst.locbash: syntax error near unexpected token `('Maybe I am not using egrep correctly
arthurd
since you are using bash, you'll have to escape all special characters: for example, replace \d with \\d
Etan
+2  A: 

Something like:

app/(.*)/oracle

Would do the trick.

cyborg
+2  A: 

Try this, assuming your egrep has -o:

$ echo '/u01/app/ORAENV/oracle/oraInventory' | egrep -o '/app/[0-9A-Za-z]+/oracle/' | cut -d'/' -f3

Output:

ORAENV

Update, solution using sed:

$ echo '/u01/app/ORAENV/oracle/oraInventory' | sed 's:.*/app/\(.*\)/oracle/.*:\1:'
Mark Byers
Thanks for the suggestion unfortunately my egrep does not support "-o". Gotta love solaris standard tools... the bane of my life lately.
arthurd
Perhaps grep is not the right tool for you. How about sed instead?
Mark Byers
The sed example works great!. Thanks
arthurd
A: 
$ echo "inventory_loc=/u01/app/ORAENV/oracle/oraInventory"| nawk -F"/" '{for(i=1;i<=NF;i++)if($i=="app") {print $(i+1);exit} } '
ORAENV
ghostdog74