Extract substring in Bash

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

    Use cut:

    echo 'someletters_12345_moreleters.ext' | cut -d'_' -f 2
    

    More generic:

    INPUT='someletters_12345_moreleters.ext'
    SUBSTRING=$(echo $INPUT| cut -d'_' -f 2)
    echo $SUBSTRING
    

提交回复
热议问题