Extract substring in Bash

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

    A little late, but I just ran across this problem and found the following:

    host:/tmp$ asd=someletters_12345_moreleters.ext 
    host:/tmp$ echo `expr $asd : '.*_\(.*\)_'`
    12345
    host:/tmp$ 
    

    I used it to get millisecond resolution on an embedded system that does not have %N for date:

    set `grep "now at" /proc/timer_list`
    nano=$3
    fraction=`expr $nano : '.*\(...\)......'`
    $debug nano is $nano, fraction is $fraction
    

提交回复
热议问题