If x is constant, the following parameter expansion performs substring extraction:
(如果x为常数,则以下参数扩展将执行子字符串提取:)
b=${a:12:5}
where 12 is the offset (zero-based) and 5 is the length
(其中12是偏移量(从零开始),而5是长度)
If the underscores around the digits are the only ones in the input, you can strip off the prefix and suffix (respectively) in two steps:
(如果数字中的下划线是输入中唯一的数字,则可以分两步(分别)去除前缀和后缀:)
tmp=${a#*_} # remove prefix ending in "_"
b=${tmp%_*} # remove suffix starting with "_"
If there are other underscores, it's probably feasible anyway, albeit more tricky.
(如果还有其他下划线,尽管比较棘手,但这仍然是可行的。)
If anyone knows how to perform both expansions in a single expression, I'd like to know too. (如果有人知道如何在一个表达式中执行两个扩展,我也想知道。)
Both solutions presented are pure bash, with no process spawning involved, hence very fast.
(提出的两种解决方案都是纯bash,不涉及任何流程生成,因此非常快。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…