Extract substring in Bash

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

    A bash solution:

    IFS="_" read -r x digs x <<<'someletters_12345_moreleters.ext'
    

    This will clobber a variable called x. The var x could be changed to the var _.

    input='someletters_12345_moreleters.ext'
    IFS="_" read -r _ digs _ <<<"$input"
    

提交回复
热议问题