史蒂文
是否有一个简单的命令行来提取由连字符分隔的字符串的最后一部分?例如,我想提取123
的foo-bar-123
。
杰森·沃扬
您可以使用Bash的参数扩展:
string="foo-bar-123" && printf "%s\n" "${string##*-}"
123
如果要使用其他进程,请使用Awk:
echo "foo-bar-123" | awk -F- '{print $NF}'
或者,如果您喜欢Sed:
echo "foo-bar-123" | sed 's/.*-//'
较轻的外部过程中,由于格伦杰克曼建议是cut
:
cut -d- -f3 <<< "$string"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
编辑于
我来说两句