Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting numbers from a string with grep

Tags:

grep

I came with another simple question...

I got a string with a substring in the format xx:xx:xx where the x's are numbers. I want to extract that substring including the ":" symbol, so my output would be "xx:xx:xx".

I think it can be done with a grep -Eo [0-9], but im not sure of the syntax... Any help?

like image 625
Ghost Avatar asked Oct 29 '25 03:10

Ghost


1 Answers

echo "substring in the format 12:43:37 where the x's are numbers" | 
      grep -o '[0-9:]*'

Output:

12:43:37

If you have other numbers in the input string you can be more specific:

grep -o '[0-9]*:[0-9]*:[0-9]*'

even:

grep -o '[0-9][0-9]:[0-9][0-9]:[0-9][0-9]'
like image 68
perreal Avatar answered Oct 31 '25 02:10

perreal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!