Answers:
的后三个字符string
:
${string: -3}
要么
${string:(-3)}
(介意之间的空间:
和-3
在所述第一形式)。
${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.
由于这个答案得到了一些常规的见解,因此让我添加一种可能性来解决约翰·里克斯的评论;正如他提到的,如果您的字符串长度小于3,则${string: -3}
扩展为空字符串。在这种情况下,如果您想扩展string
,则可以使用:
${string:${#string}<3?0:-3}
这使用?:
三元if运算符,可以在Shell Arithmetic中使用;由于如前所述,偏移量是一个算术表达式,因此这是有效的。
deppfx@localhost:/tmp$ echo ${$(hostname): -3}
-bash: ${$(hostname): -3}: bad substitution
temp=$(hostname); echo "${temp: -3}"
。Bash也具有HOSTNAME
变量(可能与的输出相同或不同hostname
)。如果要使用它,请执行echo "${HOSTNAME: -3}"
。
some func | echo ${string: -3}
-如何分配some func
to 的输出string
?
string=$(some func)
然后echo "${string: -3}"
。
另一个解决方法是使用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个字符。
为了概括gniourf_gniourf的问题和答案(这就是我要搜索的内容),如果要将字符范围从末尾的第7位剪切到末尾的第3位,可以使用以下语法:
${string: -7:4}
其中4是课程的长度(7-3)。
另外,虽然gniourf_gniourf的解决方案显然是最好和最整洁的,但我只是想使用cut添加一个替代解决方案:
echo $string | cut -c $((${#string}-2))-$((${#string}))
如果您通过将长度$ {#string}定义为一个单独的变量来分两行进行操作,则更具可读性。
cut
先计算开始/停止然后再在参数扩展中使用这些变量的方法结合起来(还值得一提的是cut
,bash偏移量分别从1和0开始,因此这需要计入计算,而我在这里不做):start=$((${#string}-3)); stop=$((${#string}));
然后echo ${string: $start : $stop}
vsecho $string | cut -c "$start"-"$stop"