Another method, relative to the OP's concept, is to use
For example, if you have a filename format like this:
And you wish to extract "artist", this will work:
sed -nr 's/.*\/(.*)\/.*/\1/p' <<< "$filename"
In the example above,
sed isolates the middle sub-string by removing everything left and right of the first pattern bounded by a '/' character on either side. The delimiters are also removed.
sed begins dissecting the string from right to left. Therefore, it doesn't matter if your filename path in this illustration begins with
/ or not. For instance, it returns the same result ("artist") for either of these paths:
About the command line
-n limits the output to one iteration
-r allows the use of a regex expression
/p tells sed to print the result
Also, you may prefer this slightly shorter version of the command (though it is less explicit):
sed -r 's/.*\/(.*)\/.*/\1/' <<< "$filename"
If you want to use a different character or string as boundary delimiters, change the character(s) in the position where you see the 'X' below:
sed -nr 's/.*\X(.*)\X.*/\1/p' <<< "$test"