给定someletters_12345_moreleters形式的文件名。ext,我想提取5位数字,并将它们放入一个变量。
为了强调这一点,我有一个x个字符的文件名,然后是一个5位数字序列,两边都有一个下划线,然后是另一组x个字符。我想把这个5位数代入一个变量。
我对实现这一目标的多种不同方式非常感兴趣。
给定someletters_12345_moreleters形式的文件名。ext,我想提取5位数字,并将它们放入一个变量。
为了强调这一点,我有一个x个字符的文件名,然后是一个5位数字序列,两边都有一个下划线,然后是另一组x个字符。我想把这个5位数代入一个变量。
我对实现这一目标的多种不同方式非常感兴趣。
当前回答
如果有人想要更严格的信息,你也可以像这样在man bash中搜索
$ man bash [press return key]
/substring [press return key]
[press "n" key]
[press "n" key]
[press "n" key]
[press "n" key]
结果:
${parameter:offset} ${parameter:offset:length} Substring Expansion. Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter start‐ ing at the character specified by offset. length and offset are arithmetic expressions (see ARITHMETIC EVALUATION below). If offset evaluates to a number less than zero, the value is used as an offset from the end of the value of parameter. Arithmetic expressions starting with a - must be separated by whitespace from the preceding : to be distinguished from the Use Default Values expansion. If length evaluates to a number less than zero, and parameter is not @ and not an indexed or associative array, it is interpreted as an offset from the end of the value of parameter rather than a number of characters, and the expan‐ sion is the characters between the two offsets. If parameter is @, the result is length positional parameters beginning at off‐ set. If parameter is an indexed array name subscripted by @ or *, the result is the length members of the array beginning with ${parameter[offset]}. A negative offset is taken relative to one greater than the maximum index of the specified array. Sub‐ string expansion applied to an associative array produces unde‐ fined results. Note that a negative offset must be separated from the colon by at least one space to avoid being confused with the :- expansion. Substring indexing is zero-based unless the positional parameters are used, in which case the indexing starts at 1 by default. If offset is 0, and the positional parameters are used, $0 is prefixed to the list.
其他回答
您可以使用参数展开来做到这一点。
如果a为常数,则下面的参数展开执行子字符串提取:
b=${a:12:5}
12是偏移量(从零开始),5是长度
如果数字周围的下划线是输入中唯一的下划线,您可以分两步分别去掉前缀和后缀:
tmp=${a#*_} # remove prefix ending in "_"
b=${tmp%_*} # remove suffix starting with "_"
如果有其他下划线,那么无论如何都可能是可行的,尽管比较棘手。如果有人知道如何在一个表达式中执行两个展开,我也想知道。
提出的两个解决方案都是纯bash,不涉及进程生成,因此非常快。
还有bash内置的'expr'命令:
INPUT="someletters_12345_moreleters.ext"
SUBSTRING=`expr match "$INPUT" '.*_\([[:digit:]]*\)_.*' `
echo $SUBSTRING
通用解决方案,其中数字可以在文件名中的任何位置,使用这样的序列中的第一个:
number=$(echo $filename | egrep -o '[[:digit:]]{5}' | head -n1)
另一个精确提取变量一部分的解决方案:
number=${filename:offset:length}
如果你的文件名总是使用stuff_digits_…你可以使用awk:
number=$(echo $filename | awk -F _ '{ print $2 }')
还有一种方法可以删除除数字以外的所有内容,使用
number=$(echo $filename | tr -cd '[[:digit:]]')
很多过时的解决方案都需要管道和子外壳。 自bash版本3(2004年发布)以来,它有一个内置的正则表达式比较操作符=~。
input="someletters_12345_moreleters.ext"
# match: underscore followed by 1 or more digits followed by underscore
[[ $input =~ _([0-9]+)_ ]]
echo ${BASH_REMATCH[1]}
输出:
12345
注意,如果您不是很精通编写RegExp,我建议您阅读精通正则表达式。
如果您只是需要弄清楚如何让RegExp工作,并且它不符合您的想法,请尝试RegEx101.com的在线GUI,并将“Flavor”设置为“PCRE”,以便获得bash使用的[[:digit:]]等POSIX风格的字符类。
也许这可以帮助你得到想要的输出
代码:
your_number=$(echo "someletters_12345_moreleters.ext" | grep -E -o '[0-9]{5}')
echo $your_number
输出:
12345