In bash, suppose that I have a string strname
:
strname="ph7go04325r"
I would like to extract the characters between the first "3"
character and the last "r"
character in strname
, saving the result in a string strresult
. In the example above, the resulting strresult
would be:
strresult="25"
The first "3"
character is not necessarily at string position 8 in strname
; likewise, the last "r"
is not necessarily at string position 11. Thus, both of the following strings strname
should yield strresult="25"
:
strname="ph11go04325raa"
strname="325r"
strname="rgo04325raa"
Also, strname=ph12go04330raa"
should yield strresult="30"
.
I am new to bash scripting, and I don't know where to begin to do string pattern matching like this. Do you have any suggestions?