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.
Ok, here goes pure Parameter Substitution with an empty string. Caveat is that I have defined someletters and moreletters as only characters. If they are alphanumeric, this will not work as it is.
Without any sub-processes you can:
A very small variant of this will also work in ksh93.
I'm surprised this pure bash solution didn't come up:
You probably want to reset IFS to what value it was before, or
unset IFS
afterwards!I love
sed
's capability to deal with regex groups:A slightly more general option would be not to assume that you have an underscore
_
marking the start of your digits sequence, hence for instance stripping off all non-numbers you get before your sequence:s/[^0-9]\+\([0-9]\+\).*/\1/p
.More on this, in case you're not too confident with regexps:
s
is for _s_ubstitute[0-9]+
matches 1+ digits\1
links to the group n.1 of the regex output (group 0 is the whole match, group 1 is the match within parentheses in this case)p
flag is for _p_rintingAll escapes
\
are there to makesed
's regexp processing work.Generic solution where the number can be anywhere in the filename, using the first of such sequences:
Another solution to extract exactly a part of a variable:
If your filename always have the format
stuff_digits_...
you can use awk:Yet another solution to remove everything except digits, use
Given test.txt is a file containing "ABCDEFGHIJKLMNOPQRSTUVWXYZ"