Sre*_*ree 4 unix shell substring
我有这样的字符串
this_is_test_string1_22
this_is_also_test_string12_6
Run Code Online (Sandbox Code Playgroud)
我想在最后一个下划线周围分割和提取字符串.那就是我想要这样的输出
this_is_test_string1 and 22
this_is_also_test_string12 and 6
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我如何在unix shell脚本中获得这个.
谢谢.SREE
你可以做
s='this_is_test_string1_22'
Run Code Online (Sandbox Code Playgroud)
在BASH:
echo "${s##*_}"
22
Run Code Online (Sandbox Code Playgroud)
或使用sed:
sed 's/^.*_\([^_]*\)$/\1/' <<< 'this_is_test_string1_22'
22
Run Code Online (Sandbox Code Playgroud)
编辑 sh:
echo "$s" | sed 's/^.*_\([^_]*\)$/\1/'
Run Code Online (Sandbox Code Playgroud)