seafan
March 11, 2011, 12:14pm
1
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!')
seafan:
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).
You can remove the last "/" and everything that follows it with:
echo "${LOCATION%/*}"
Regards,
Alister
seafan
March 11, 2011, 12:32pm
5
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%/*}
seafan
March 11, 2011, 1:09pm
7
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%/*}
seafan
March 11, 2011, 5:29pm
9
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 ----------