I am quite novice in bash scripting. The task I need to solve is extract a particular substring from each filename in a directory.Example file names:Bob Marley - I Shot The Sheriff-2XiYUYcpsT4.mp3Audioslave - Be Yourself-WC5FdFlUcl0.mp3Bruno Mars - Treasure [Official Music Video]-nPvuNsRccVw.mp3Basically, it is a collection of music I've fetched from youtube and I need to extract the video id (the part in bold).How can I solve this problem in bash? Splitting based on delimeter is not reliable as the video id sometimes contains an hyphen itself.Thanks
Extract a particular substring from filename
bash;files
You could use substring expansion:for file in ./*.mp3dovideoid=${file:${#file}-15:11}printf %s\\n $videoiddoneor, if more than one extension, usenoext=${file%.*}videoid=${noext:${#noext}-11:11}This assumes that youtube video IDs are always 11 characters.