Extract substring in Bash

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

    I love sed's capability to deal with regex groups:

    > var="someletters_12345_moreletters.ext"
    > digits=$( echo $var | sed "s/.*_\([0-9]\+\).*/\1/p" -n )
    > echo $digits
    12345
    

    A slightly more general option would be not to assume that you have an underscore _ marking the start of your digits sequence, hence for instance stripping off all non-numbers you get before your sequence: s/[^0-9]\+\([0-9]\+\).*/\1/p.


    > man sed | grep s/regexp/replacement -A 2
    s/regexp/replacement/
        Attempt to match regexp against the pattern space.  If successful, replace that portion matched with replacement.  The replacement may contain the special  character  &  to
        refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.
    

    More on this, in case you're not too confident with regexps:

    • s is for _s_ubstitute
    • [0-9]+ matches 1+ digits
    • \1 links to the group n.1 of the regex output (group 0 is the whole match, group 1 is the match within parentheses in this case)
    • p flag is for _p_rinting

    All escapes \ are there to make sed's regexp processing work.

提交回复
热议问题