I have a variable whose value is like this
i=/test/test1/test2/myfile.cd.070505123457
i would like to have the value of myfile.cd stored into another variable
my attempt is
test=${i##*/} ;echo $test ##and i get
myfile.cd.070505123457
since what i wnat is myfile.cd i try this
test={{${i##*/}}##.} ;echo $test
and i get an error
I have two solutions from you ..but before you bring out your magic wand again..can you share the trick please or better if you could pass on the magic wand itself
`expr' supports pattern matching and other string operators.
`STRING : REGEX'
Perform pattern matching. The arguments are converted to strings
and the second is considered to be a (basic, a la GNU `grep')
regular expression, with a `^' implicitly prepended. The first
argument is then matched against this regular expression.
If the match succeeds and REGEX uses `\(' and `\)', the `:'
expression returns the part of STRING that matched the
subexpression.
And...
`basename' removes any leading directory components from NAME.
Synopsis:
basename NAME [SUFFIX]
If SUFFIX is specified and is identical to the end of NAME, it is
removed from NAME as well.
The following solution works fine under zsh ( but i don't think that there is many people using this shell), others shells give the error 'bad substitution' :
Well maybe this will help... the pieces in red are special:
'.*/\(.*\)\..'
They define the beginning and end of a saved field. This is what will be be returned. The stuff to letf of the saved field is ./ which matches everything up to a slash. Inside the saved field we have .* which seems to say match any characters. But we can't match literally everything, we have to save (just) enough for the rest of the expression to work. So after the saved field we have \..* which says match a dot followed by any characters.
Regular expression quantifiers are greedy, so "*" means match as much as possible.
./ Matches any characters upto the last /
\(.\)\. Matches and returns any characters upto the last .
.* not actually required. (I originally thought that this was required because expr regular expressions are anchored, but it turns out that they are only achored to the start of the expression.)