Extract substring in Bash

前端 未结 22 1902
别那么骄傲
别那么骄傲 2020-11-22 11:02

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

22条回答
  •  长发绾君心
    2020-11-22 11:30

    I'm surprised this pure bash solution didn't come up:

    a="someletters_12345_moreleters.ext"
    IFS="_"
    set $a
    echo $2
    # prints 12345
    

    You probably want to reset IFS to what value it was before, or unset IFS afterwards!

提交回复
热议问题