如何在bash中查找字符串中的最后一个分组数字 [英] how to find the last grouped digit in a string in bash
本文介绍了如何在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 onlytail -1
output only the last match
好吧,如果您有字符串:
well, if you have string:
grep -o '[0-9]\+' <<< '123 foo 456 bar' |tail -1
这篇关于如何在bash中查找字符串中的最后一个分组数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文