shell脚本字符串删除
How to delete or get the number in the tail from a string in shell script (bash script)? Okay to call other tools.
如何从shell脚本(bash脚本)的字符串中删除或获取尾部的数字? 可以调用其他工具 。
For example, from “/dev/sda8”, I want to get “/dev/sda” and “8” separately.
例如,我要从“ / dev / sda8”分别获得“ / dev / sda”和“ 8”。
This can be achieved by using sed
as follows.
可以通过如下使用sed
来实现。
To get the string after deleting the tailing numbers, we can use sed
to replace the tailing numbers with an empty string.
要在删除尾号后获得字符串,我们可以使用sed
将尾号替换为空字符串。
str=sda8
# get the string with the number tail
strhead=$(echo "sda8" | sed 's/[0-9]*$//')
To get the tailing numbers by making use of the strhead
we got, by getting the substring from the original string from the position after the strhead
.
要通过使用strhead
来获取strhead
,是从strhead
之后的位置从原始字符串中获取子字符串。
strtail=${str:${#strhead}}
After these operations, we can get the split strings.
完成这些操作后,我们可以获得拆分后的字符串。
$ echo $strhead
sda
$ echo $strtail
8
shell脚本字符串删除