Extract substring in Bash

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

    Without any sub-processes you can:

    shopt -s extglob
    front=${input%%_+([a-zA-Z]).*}
    digits=${front##+([a-zA-Z])_}
    

    A very small variant of this will also work in ksh93.

提交回复
热议问题