Is it possible to apply two matching pattern transformations in one operation?
bash, shell
Solution
echo "1:"
if [[ "$SONG" =~ \~/Music/Mine/(.*)\.flac ]] ; then SONG=${BASH_REMATCH[1]} ; fi
echo $SONG
echo "2:"
[[ "$SONG" =~ \~/Music/Mine/(.*)\.flac ]] && SONG=${BASH_REMATCH[1]}
echo $SONG
1 and 2 use bash regular expressions. The first example has the added advantage of being able to break into an `else` branch if your string doesn't match the format thats expected.* The second example is a bit cleaner. In both cases, if `${SONG}` doesn't match the pattern, it is left unchanged.
But using awk or sed might be easier to understand. For example:
echo "3:"
SONG=$(echo "$SONG" | sed -r 's:~/Music/Mine/(.*)\.flac:\1:')
echo $SONG
[*] See DennisWilliamson's note below regarding using `||` to get an `else` branch.
Problem
I have a bash variable populated with a filename and path: ``` SONG="~/Music/Mine/Cool Title Bro.flac" ``` In my attempts to make tagging dramatically easier, I applied a bit of transformation to the variable to isolate the title: ``` echo "${SONG#\~/Music/Mine/}" # which prints: Cool Title Bro.flac ``` I know it's also possible to remove the suffix with `${SONG%%.flac}`. But is it possible to remove both the prefix and the suffix in a single operation? This: ``` ${SONG#\~/Music/Mine/%%.flac} ``` doesn't work presumably because it tries to match a literal `%%.flac` as part of the prefix. The reverse does not work (`%%.flac#~/[...]`), and I've even gone crazy and tried ``` ${${SONG#~/Music/Mine/}%%.flac} ``` which also does not work. This may be a prime example of over-engineering on my part, but it'd be excellent if there is a way to do this and I just haven't figured it out yet.