I downloaded some files from YouTube a while back with the YouTube IDs in their names. My problem is that some IDs contain a character that also marks the beginning of the ID itself.
$ ls *.mp4 | head -n 2 1 - How to make an operating system from scratch-rr-9w2gITDM.mp4 2 - How to make an operating system from scratch-WwitBbsUvc8.mp4 I want to return the IDs rr-9w2gITDM and WwitBbsUvc8.
In the following two examples, what I have written work for the second ID, but not the first. They seem to be greedily matching up to the last matching character. Using /1 at the end did not make any difference.
$ ls *.mp4 | sed -e 's/^.*\-//1;s/\..*$//1' 9w2gITDM WwitBbsUvc8 $ ls *.mp4 | sed -e 's/^.*\-\(.*\)\..*$/\1/' 9w2gITDM WwitBbsUvc8 I prefer to solve this using macOS sed, but I'm also open to using gsed (GNU sed) if no other option is possible.
[^-]*), but that would get you in trouble if the actual name contains a dash. But since Youtube IDs always have 11 characters, maybe match everything up to dash + 11 characters +.mp4?