sed提取数字组 [英] sed extracting group of digits

查看:277
本文介绍了sed提取数字组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试提取下面给出的数字,但屏幕上没有任何打印内容

I have tried to extract a number as given below but nothing is printed on screen:

echo "This is an example: 65 apples" | sed -n  's/.*\([0-9]*\) apples/\1/p'

但是,如果两个数字分别如下匹配,我将得到'65':

However, I get '65', if both digits are matched separately as given below:

echo "This is an example: 65 apples" | sed -n  's/.*\([0-9][0-9]\) apples/\1/p'
65

我该如何匹配一个数字,使得我不知道要提取的数字中的位数,例如可以用2344代替65?

How can I match a number such that I don't know the number of digits in a number to be extracted e.g. it can be 2344 in place of 65?

推荐答案

$ echo "This is an example: 65 apples" | sed -r  's/^[^0-9]*([0-9]+).*/\1/'
65

这篇关于sed提取数字组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆