如何分割壳串并获得最后一个字段 [英] how to split a string in shell and get the last field

查看:167
本文介绍了如何分割壳串并获得最后一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我有字符串 1:2:3:4:5 ,我想获得它的最后一个字段( 5 在这种情况下)。我该怎么做,使用bash?我试图剪切,但我不知道如何与 -f 指定的最后一个字段。

suppose I have the string 1:2:3:4:5 and I want to get its last field (5 in this case). how do I do that using Bash? I tried cut, but I don't know how to specify the last field with -f.

推荐答案

您可以使用字符串操作符

$ foo=1:2:3:4:5
$ echo ${foo##*:}
5

这一切修剪从正面,直到:,贪婪地

This trims everything from the front until a ':', greedily.

${foo  <-- from variable foo
  ##   <-- greedy front trim
  *    <-- matches anything
  :    <-- until the last ':'
 }

这篇关于如何分割壳串并获得最后一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆