Bash: How to extract numbers preceded by _ and followed by

Viewed 1615

I have the following format for filenames: filename_1234.svg

How can I retrieve the numbers preceded by an underscore and followed by a dot. There can be between one to four numbers before the .svg

I have tried:

width=${fileName//[^0-9]/}

but if the fileName contains a number as well, it will return all numbers in the filename, e.g.

file6name_1234.svg

I found solutions for two underscores (and splitting it into an array), but I am looking for a way to check for the underscore as well as the dot.

6 Answers
Related