linux - sed 提取数字组

标签 linux sed

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

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/

相关文章:

c - 从标准 C 中的字符串中删除字符

sed - 使用 sed 在数字和文本之间添加空格

linux - 使用 bash 从文本文件中提取单词

apache - 在带有引号和转义字符的 Alias 中使用 sed

sed - grep/sed : How to print *something* when nothing matches

linux - 如何在同一个linux系统上拥有不同的lua版本

linux - 我可以使用 sed 重命名带有 "running count"的文件吗?

linux - 是否可以在 CentOS 7 上使用 JFS?

python - 使用 python superprocess.call 方法时出错,

linux - 使用 grep/linux/bash 计算重复字符串的重叠出现次数