1

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.

arthurd
  • 13
  • 2

4 Answers4

3

Try this:

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

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • bash-3.00# egrep /app/([\d\w]+)/ /var/opt/oracle/oraInst.loc bash: syntax error near unexpected token `(' Is this a problem with egrep? – arthurd Dec 20 '09 at 23:45
  • bash-3.00# egrep \/app\/([\d\w]+)\/oracle\/ /var/opt/oracle/oraInst.loc bash: syntax error near unexpected token `(' Maybe I am not using egrep correctly – arthurd Dec 20 '09 at 23:50
  • since you are using bash, you'll have to escape all special characters: for example, replace \d with \\d – Etan Dec 20 '09 at 23:54
2

Something like:

app/(.*)/oracle

Would do the trick.

cyborg
  • 5,638
  • 1
  • 19
  • 25
2

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
  • 811,555
  • 193
  • 1,581
  • 1,452
0
$ 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
  • 327,991
  • 56
  • 259
  • 343