Search code examples
bashsedgrepfile-rename

Extract numbers from filenames disregarding extensions


I'm making a script to rename some video files. Some are named XXX blah blah.ext and some are XXX - XXX blah blah.ext where "X" are digits. Furthermore, some files are .avi and some are mp4. What I'd like is to extract the numbers from these files, separated by a space if there is more than one, and to disregard the "4" in ".mp4" files.

My current implementation is egrep -o "[[:digit:]]*", and while this does separate numbers into different outputs, it also considers ".mp4".

Using sed I've not only not been able to produce different outputs for every number, but it also includes the "4". Note: I'm very new to sed i.e. I began learning it for the purpose of writing this script.

How can I do this?


Solution

  • for file in *
    do
        echo $file | sed 's/\..*$//' | egrep -o "[[:digit:]]*"
    done