Extract substring in Bash

前端 未结 22 2198
别那么骄傲
别那么骄傲 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:39

    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.

    filename=someletters_12345_moreletters.ext
    substring=${filename//@(+([a-z])_|_+([a-z]).*)}
    echo $substring
    12345
    

提交回复
热议问题