如何在unix shell脚本中的最后一个下划线(_)之后获取子字符串 [英] How to get a substring after the last underscore (_) in unix shell script
本文介绍了如何在unix shell脚本中的最后一个下划线(_)之后获取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的字符串
this_is_test_string1_22
this_is_also_test_string12_6
我想拆分并提取最后一个下划线周围的字符串.那就是我想要这样的输出
I wanted to split and extracts string around the last underscore. That is i wanted the outputs like this
this_is_test_string1 and 22
this_is_also_test_string12 and 6
谁能帮助我如何在 unix shell 脚本中获得它.
Can anyone help me how to get this in unix shell scripting.
谢谢.斯雷
推荐答案
你可以做
s='this_is_test_string1_22'
在 BASH 中:
echo "${s##*_}"
22
或使用 sed:
sed 's/^.*_\([^_]*\)$/\1/' <<< 'this_is_test_string1_22'
22
编辑 sh:
echo "$s" | sed 's/^.*_\([^_]*\)$/\1/'
这篇关于如何在unix shell脚本中的最后一个下划线(_)之后获取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文