我尝试提取下面给出的数字,但屏幕上没有打印任何内容:
echo "This is an example: 65 apples" | sed -n 's/.*\([0-9]*\) apples/\1/p'
但是,如果两个数字分别匹配,我会得到“65”,如下所示:
echo "This is an example: 65 apples" | sed -n 's/.*\([0-9][0-9]\) apples/\1/p'
65
如何匹配一个数字,这样我就不知道要提取的数字中的位数,例如可以用 2344 代替 65 吗?
最佳答案
$ echo "This is an example: 65 apples" | sed -r 's/^[^0-9]*([0-9]+).*/\1/'
65
关于linux - sed 提取数字组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9260625/