在Unix上匹配文件中的日期 [英] Matching a date in a file on Unix

查看:84
本文介绍了在Unix上匹配文件中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下unix命令,该命令用于尝试在文件中查找格式为yyyy-mm-dd的日期:

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> 

但是由于某种原因,我得到的答案是空的.我是否为grep正确匹配了正则表达式?

but for some reason I'm getting an empty answer. Am I matching the regex correctly for grep?

推荐答案

使用bash扩展正则表达式(-E--extended-regexp),以下工作正常:

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> 

但是,在这种情况下,您应该使用[0-9]而不是\d.

But, in this case you should use [0-9] instead of \d.

如果要使用\d,则需要指定PERL正则表达式(-P--perl-regexp):

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> 

这篇关于在Unix上匹配文件中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆