Reputation: 29465
I would like to extract a string as below:
setenv VARIABLE /opt/Application/File/Version22
I want to display only
File/Version22
on screen. It means /opt/Application/
is always the prefix. I want to display the rest of the string. How do I do it in csh?
Regards
Upvotes: 0
Views: 12098
Reputation: 263537
Now that you've updated the question (thank you for that), it's clear that you always want to remove the /opt/Application
prefix.
The most straightforward way to do that, which will work in any shell, is:
echo $VARIABLE | sed 's|^/opt/Application/||'
(It's usual to use /
as a delimiter for replacements like this, but you can use any punctuation character; I'm using |
to avoid conflicting with the /
characters in the pattern.)
A more tcsh-specific, and possibly more efficient, way to do it is:
echo $VARIABLE:s|/opt/Application/||
It's likely to be more efficient because it's done within the shell and avoids invoking the external sed
command. On the other hand, the overhead of executing sed
is unlikely to be significant.
Note carefully that the :s
syntax is not supported in the original csh. It is supported in tcsh and in some newer versions of csh. If you want to do this portably, just use sed
.
Upvotes: 1
Reputation: 3071
C-Shell has the built-in string modifiers that can do this with very little code:
echo $VARIABLE:h:t/$VARIABLE:t
string modifiers:
:h = remove the last directory (aka head)
:t = remove every directory except the last one (aka tail)
Upvotes: 1
Reputation: 16748
I assume that you want to get the 2 last names in a filename:
setenv VARIABLE /opt/Application/File/Version22
set lastfile=`basename $VARIABLE`
set prevpath=`dirname $VARIABLE`
set previousfile=`basename $prevpath`
set file=$previousfile/$lastfile
echo $file
Upvotes: 0