使用 grep 从字符串中获取数字

Gho*_*ost 4 grep

我带着另一个简单的问题来了......

我得到一个带有 xx:xx:xx 格式子字符串的字符串,其中 x 是数字。我想提取包含“:”符号的子字符串,因此我的输出将是“xx:xx:xx”。

我认为可以用 grep -Eo [0-9] 来完成,但我不确定语法......有什么帮助吗?

per*_*eal 7

echo "substring in the format 12:43:37 where the x's are numbers" | 
      grep -o '[0-9:]*'
Run Code Online (Sandbox Code Playgroud)

输出:

12:43:37
Run Code Online (Sandbox Code Playgroud)

如果输入字符串中有其他数字,您可以更具体:

grep -o '[0-9]*:[0-9]*:[0-9]*'
Run Code Online (Sandbox Code Playgroud)

甚至:

grep -o '[0-9][0-9]:[0-9][0-9]:[0-9][0-9]'
Run Code Online (Sandbox Code Playgroud)