如何在bash中查找字符串中的最后一个分组数字
问题描述:
这是此问题的后续问题,有关如何知道字符串中分组数字的数量.
This is a follow-up question to this question, regarding how to know the number of grouped digits in string.
在bash中,
如何找到字符串中最后出现的一组数字? 所以,如果我有
How can I find the last occurrence of a group of digits in a string? So, if I have
string="123 abc 456"
我会得到
456
如果我有
string="123 123 456"
我仍然会得到
456
答
grep -o '[0-9]\+' file|tail -1
-
grep -o
仅列出匹配的文本 -
tail -1
仅输出最后一个匹配项 -
grep -o
lists matched text only -
tail -1
output only the last match
好吧,如果您有字符串:
well, if you have string:
grep -o '[0-9]\+' <<< '123 foo 456 bar' |tail -1