how to remove this substring?

Hello,

I need to remove a substring from a string in a ksh script, the string is like this:

LOCATION=+DATADG1/CMSPRD1/datafile/system.235.456721

or

LOCATION=/u03/oradata/CMSPRD/SYSTEM.dbf

I need to strip the last file name from that path, and get:

+DATADG1/CMSPRD1/datafile

or,

/u03/oradata/CMSPRD

I experimented with

expr index "$LOCATION" SYSTEM

trying to get the position of the "system" string and then use

substr $LOCATION $position

but the index part didn't work to begin with.

I also experimented with:

echo ${LOCATION#system}
echo ${LOCATION%%SYSTEM}

they didn't work to substract "sytem" or "SYSTEM" (they could be either lower case or upper case).

Your help is much appreciated.

-Jay

Try DIR=$(basedir $STRING)

Or you can do something like:

string='LOCATION=/u03/oradata/CMSPRD/SYSTEM.dbf'
newstring=$(echo "$string" | sed 's!.*=\(.*\)/.*!\1!')

You can remove the last "/" and everything that follows it with:

echo "${LOCATION%/*}"

Regards,
Alister

Thanks for your quick reply. It looks that I don't have basedir in ksh:

$ basedir $LOC
ksh: basedir: not found

but I have basename:

LOC=/db1/data/CMSDEV/SYSTEM.dbf

$ print $(basename ${LOC})
SYSTEM.dbf

That gets me the file name, while I need the dir path/name.

string='LOCATION=/u03/oradata/CMSPRD/SYSTEM.dbf'
x=${string#*=}
newstring=${x%/*}

edit: Misunderstood question.

Try:

echo ${LOCATION%/*}

echo "${LOCATION%/*}"
returns empty/null

---------- Post updated at 12:09 PM ---------- Previous update was at 11:44 AM ----------

this returns empty/null as well:

$ echo "$LOCATION" | sed 's!.*=\(.*\)/.*!\1!'

which makes me wonder if there is some shell env variable that needs to be set for this and the previous

echo "${LOCATION%/*}"

?

I've misread the question, the sed command should be:

echo "$LOCATION" | sed 's!\(.*\)/.*!\1!'

But have you tried this?

echo ${LOCATION%/*}

Both are working now, after I logged out and logged in again. Thank you all so much for helping. This was my first post on this site, you guys are great!

now I'm also curious, why that expr index $LOCATION SYSTEM didn't work?

---------- Post updated at 04:29 PM ---------- Previous update was at 03:32 PM ----------