Article 51J64 [SOLVED] BASH Script to extract numerics from a filename

[SOLVED] BASH Script to extract numerics from a filename

by
GPGAgent
from LinuxQuestions.org on (#51J64)
I can extract the digits only from a file name but I need to use an intermediate variable, how can I do this in a single step - bash string handling is really powerful but even with 40+ years of programming in other languages I just cannot get to grips with it :-(

This is the info where the filename Ep02.MP2 is held in variable X:
Code:charlie@charlie-machine:~/MP2-MP3/Test$ echo $X
Ep02.MP2
charlie@charlie-machine:~/MP2-MP3/Test$ echo ${X%.*}
Ep02
charlie@charlie-machine:~/MP2-MP3/Test$ Y=${X%.*}
charlie@charlie-machine:~/MP2-MP3/Test$ echo ${Y//[^0-9]/ }
02
charlie@charlie-machine:~/MP2-MP3/Test$Whatever I try I get errors for example:
Code:charlie@charlie-machine:~/MP2-MP3/Test$ echo ${"${X%.*}"//[^0-9]/ }
bash: ${"${X%.*}"//[^0-9]/ }: bad substitution
charlie@charlie-machine:~/MP2-MP3/Test$latest?d=yIl2AUoC8zA latest?i=YQJpnNIxf3E:YIjGr-09VFQ:F7zBnMy latest?i=YQJpnNIxf3E:YIjGr-09VFQ:V_sGLiP latest?d=qj6IDK7rITs latest?i=YQJpnNIxf3E:YIjGr-09VFQ:gIN9vFwYQJpnNIxf3E
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments