shell脚本字符串删除_如何从Shell脚本中的字符串中删除或获取尾数?

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

翻译自: https://www.systutorials.com/how-to-delete-or-get-the-number-in-the-tail-from-a-string-in-shell-script/

shell脚本字符串删除

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值