I'm trying to get a partial file path by passing the part I want removed to sed. Sed gets garbled when I try multiple directories (i.e. because of the extra slash).
For example:
FULLFILEPATH="usr/local/bin"
STRIPDIR="usr"
PARTFILEPATH=`echo $FULLFILEPATH | sed s/\${STRIPDIR}//`
Gives me /local/bin. That works fine. But if I try:
FULLFILEPATH="usr/local/bin"
STRIPDIR="usr/local"
PARTFILEPATH=`echo $FULLFILEPATH | sed s/\${STRIPDIR}//`
Sed gets garbled. Any other ways I can accomplish this with or without sed?
To do this with sed I would switch delimiters. The backslash before the dollar sign in the sed command doesn't seem to be needed. On the other hand, I like superfluous double quotes around the sed argument. So my code would be:
FULLFILEPATH="usr/local/bin"
STRIPDIR="usr/local"
PARTFILEPATH=`echo $FULLFILEPATH | sed "s=${STRIPDIR}=="`
But ksh can this with built-ins. This save launching a sed process:
PARTFILEPATH=${FULLFILEPATH#$STRIPTDIR}
Several other modern shells have borrowed this from ksh so it may work with your shell.
That's exactly what I was looking for. With the sed example, I found it useful to set a variable to an unprintable character first:
CA=`echo '\001'`
and then use that for my delimiter:
PARTFILEPATH=`echo $FULLFILEPATH | sed "s${CA}${STRIPDIR}${CA}${CA}"`
I'm always a little weary of using printable characters for things like this, you never know where the one you choose might show up! I did like the ksh example even better though, I hadn't even thought of that approach.