I have the following unix command, which I'm using to try finding a date in the format yyyy-mm-dd in a file:
grep -i -w [\d]{4}-[\d]{2}-[\d]{2}? <filename>
but for some reason I'm getting an empty answer. Am I matching the regex correctly for grep?
The following is working, using bash extended regex (-E, --extended-regexp):
grep -E -i -w "[0-9]{4}-[0-9]{2}-[0-9]{2}" <filename>
But, in this case you should use [0-9] instead of \d.
If you want to use \d, you need to specify the PERL regex (-P, --perl-regexp):
grep -P -i -w "\d{4}-\d{2}-\d{2}" <filename>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With