166

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

4

4 回答 4

314

的最后三个字符string

${string: -3}

或者

${string:(-3)}

(注意第一种形式之间的空格:-3

请参考参考手册中的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.

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

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

这使用了?:三元 if 运算符,可以在Shell Arithmetic中使用;因为如记录的那样,偏移量是一个算术表达式,这是有效的。


符合 POSIX 的解决方案的更新

上一部分给出了使用 Bash 时的最佳选择。如果您想针对 POSIX shell,这里有一个选项(不使用管道或外部工具,如cut):

# New variable with 3 last characters removed
prefix=${string%???}
# The new string is obtained by removing the prefix a from string
newstring=${string#"$prefix"}

这里要观察的主要事情之一是在参数扩展中使用prefix 引号这在POSIX 参考文献(在本节末尾)中提到:

以下四种参数扩展提供了子字符串处理。在每种情况下,应使用模式匹配表示法(参见模式匹配表示法)而不是正则表达式表示法来评估模式。如果参数是“#”、“*”或“@”,则扩展的结果是未指定的。如果未设置参数并且设置 -u 有效,则扩展将失败。将完整的参数扩展字符串括在双引号中不应导致以下四种模式字符被引用,而大括号内的引号字符应具有此效果。在每个品种中,如果省略单词,则应使用空模式。

如果您的字符串包含特殊字符,这一点很重要。例如(破折号),

$ string="hello*ext"
$ prefix=${string%???}
$ # Without quotes (WRONG)
$ echo "${string#$prefix}"
*ext
$ # With quotes (CORRECT)
$ echo "${string#"$prefix"}"
ext

当然,这只有在事先知道字符数时才可用,因为您必须?在参数扩展中硬编码数字;但在这种情况下,它是一个很好的便携式解决方案。

于 2013-11-08T12:03:28.870 回答
59

您可以使用tail

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

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

echo $foo | rev | cut -c1-3 | rev
于 2013-11-08T12:01:57.973 回答
17

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

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

为了使其可选地获取最后 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

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

于 2016-08-26T12:22:12.633 回答
10

1. 广义子串

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

${string: -7:4}

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

2.使用cut替代

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

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

这里,${#string}是字符串的长度,后面的“-”表示切到最后。

3.替代使用awk

此解决方案改为使用 awk 的子字符串函数来选择一个子字符串,substr(string, start, length)如果省略长度,该子字符串的语法将结束。 length($string)-2)因此拿起最后三个字符。

echo $string | awk '{print substr($1,length($1)-2) }'
于 2017-09-19T05:20:03.240 回答