Given a filename in the form someletters_12345_moreleters.ext
, I want to extract the 5 digits and put them into a variable.
So to emphasize the point, I have a filename with x number of characters then a five digit sequence surrounded by a single underscore on either side then another set of x number of characters. I want to take the 5 digit number and put that into a variable.
I am very interested in the number of different ways that this can be accomplished.
just try to use
cut -c startIndx-stopIndx
My answer will have more control on what you want out of your string. Here is the code on how you can extract
12345
out of your stringThis will be more efficient if you want to extract something that has any chars like
abc
or any special characters like_
or-
. For example: If your string is like this and you want everything that is aftersomeletters_
and before_moreleters.ext
:With my code you can mention what exactly you want. Explanation:
#*
It will remove the preceding string including the matching key. Here the key we mentioned is_
%
It will remove the following string including the matching key. Here the key we mentioned is '_more*'Do some experiments yourself and you would find this interesting.