使用 grep,如何从文本块中提取每个数字?

Using grep, how can I extract every number from a blob of text?

与之前的 question 不同,我想在命令行上执行此操作(只需 grep)。

如何从文本文件中 grep 每个 数字并显示结果?

示例文本:

This is a sentence with 1 number, while the number 2 appears here, too.

我希望能够从文本中提取“1”和“2”(当然,我的实际文本要长得多)。

我想你想要这样的东西,

$ echo 'This is a sentence with 1 number, while the number 2 appears here, too.' | grep -o '[0-9]\+'
1
2

因为基本sed使用BRE基本正则表达式),你需要转义+符号,这样它会重复前一个字符或更多次。