arthurd
arthurd

Reputation: 13

Regular Expressions Match Specific Location In File

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.

Upvotes: 1

Views: 257

Answers (4)

ghostdog74
ghostdog74

Reputation: 342413

$ echo "inventory_loc=/u01/app/ORAENV/oracle/oraInventory"| nawk -F"/" '{for(i=1;i<=NF;i++)if($i=="app") {print $(i+1);exit} } '
ORAENV

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838296

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:'

Upvotes: 2

cyborg
cyborg

Reputation: 5748

Something like:

app/(.*)/oracle

Would do the trick.

Upvotes: 2

Andrew Hare
Andrew Hare

Reputation: 351526

Try this:

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

Upvotes: 3

Related Questions