Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching a date in a file on Unix

Tags:

grep

find

unix

sed

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?

like image 794
cybertextron Avatar asked Dec 06 '25 19:12

cybertextron


1 Answers

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> 
like image 155
JosEduSol Avatar answered Dec 09 '25 18:12

JosEduSol



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!