regex - Get digit from filename immediately preceeding file extension, with other digits in filename -


i'm trying extract last number before file extension in bash script. format varies it'll combination of numbers , letters, , last character digit. need pull digits , store them in variable.

the format generally:

sdflkej10_sdlkei450_sdlekr_1.txt 

i want store final digit 1 variable.

i'll using loop through large number of files, , last number double , triple digits.

so file:

kej10_sdlkei450_sdlekr_310.txt 

i'd need return 310.

the number of alphanumeric characters , underscores varies each file, number want before .txt extension , after underscore.

i tried:

bname=${f%%.*} number=$(echo $bname | tr -cd '[[:digit:]]')  

but returns digits.

if try

number = $(echo $(bname -2) changes number returns. 

the problem i'm having related variability, , fact i've been asked in bash. appreciated.

regex='([0-9]+)\.[^.]*$' [[ $file =~ $regex ]] && number=${bash_rematch[1]} 

this uses bash's underappreciated =~ regex operator stores matches in array named bash_rematch.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -