string 在 Bash 中访问字符串的最后 x 个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19858600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:09:31  来源:igfitidea点击:

Accessing last x characters of a string in Bash

stringbashextract

提问by aldorado

I found out that with ${string:0:3}one can access the first 3 characters of a string. Is there a equivalently easy method to access the last three characters?

我发现${string:0:3}可以访问字符串的前 3 个字符。是否有同样简单的方法来访问最后三个字符?

回答by gniourf_gniourf

Last three characters of string:

的最后三个字符string

${string: -3}

or

或者

${string:(-3)}

(mind the space between :and -3in the first form).

(注意第一种形式中:和之间的空间-3)。

Please refer to the Shell Parameter Expansion in the reference manual:

请参考参考手册中Shell Parameter Expansion

${parameter:offset}
${parameter:offset:length}

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
starting at the character specified by offset. length and offset are arithmetic
expressions (see Shell Arithmetic). This is referred to as Substring Expansion.

If offset evaluates to a number less than zero, the value is used as an offset
from the end of the value of parameter. 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 expansion is the characters between the two
offsets. If parameter is ‘@', the result is length positional parameters
beginning at offset. 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. Substring expansion applied to an
associative array produces undefined 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,
$@ is prefixed to the list.


Since this answer gets a few regular views, let me add a possibility to address John Rix's comment; as he mentions, if your string has length less than 3, ${string: -3}expands to the empty string. If, in this case, you want the expansion of string, you may use:

由于这个答案获得了一些常规观点,让我添加一个可能性来解决John Rix的评论;正如他所提到的,如果您的字符串长度小于 3,则${string: -3}扩展为空字符串。在这种情况下,如果您想要扩展string,您可以使用:

${string:${#string}<3?0:-3}

This uses the ?:ternary if operator, that may be used in Shell Arithmetic; since as documented, the offset is an arithmetic expression, this is valid.

这使用了?:三元 if 运算符,可以在Shell Arithmetic 中使用;由于如文档所述,偏移量是一个算术表达式,因此这是有效的。

回答by devnull

You can use tail:

您可以使用tail

$ foo="1234567890"
$ echo -n $foo | tail -c 3
890

A somewhat roundabout way to get the last three characters would be to say:

获得最后三个字符的一种有点迂回的方法是说:

echo $foo | rev | cut -c1-3 | rev

回答by Aurelio Jargas

Another workaround is to use grep -owith a little regex magic to get three chars followed by the end of line:

另一种解决方法是使用grep -o一些正则表达式魔术来获取三个字符,然后是行尾:

$ foo=1234567890
$ echo $foo | grep -o ...$
890

To make it optionally get the 1 to 3 last chars, in case of strings with less than 3 chars, you can use egrepwith this regex:

为了让它有选择地获取最后 1 到 3 个字符,如果字符串少于 3 个字符,您可以使用egrep此正则表达式:

$ echo a | egrep -o '.{1,3}$'
a
$ echo ab | egrep -o '.{1,3}$'
ab
$ echo abc | egrep -o '.{1,3}$'
abc
$ echo abcd | egrep -o '.{1,3}$'
bcd

You can also use different ranges, such as 5,10to get the last five to ten chars.

您还可以使用不同的范围,例如5,10获取最后五到十个字符。

回答by Adrian Tompkins

To generalise the question and the answer of gniourf_gniourf (as this is what I was searching for), if you want to cut a rangeof characters from, say, 7th from the end to 3rd from the end, you can use this syntax:

为了概括 gniourf_gniourf 的问题和答案(因为这是我正在寻找的),如果您想将一系列字符从末尾的第 7 个剪切到从末尾的第 3 个,您可以使用以下语法:

${string: -7:4}

Where 4 is the length of course (7-3).

其中 4 是课程的长度(7-3)。

In addition, while the solution of gniourf_gniourf is obviously the best and neatest, I just wanted to add an alternative solution using cut:

此外,虽然 gniourf_gniourf 的解决方案显然是最好和最简洁的,但我只想使用cut添加一个替代解决方案:

echo $string | cut -c $((${#string}-2))-$((${#string}))

This is more readable if you do it in two lines by defining the length ${#string} as a separate variable.

如果通过将长度 ${#string} 定义为单独的变量来分两行进行,这将更具可读性。