Awk数据匹配不能使用regexp + date函数

时间:2021-05-26 16:03:58

I'm trying the following regular expression for matching dates, I'll use this regular expression in the bigger awk script, but this seems to not work some how.

我正在尝试下面的正则表达式来匹配日期,我将在更大的awk脚本中使用这个正则表达式,但是这似乎没有什么作用。

echo "2343-23-23"| nawk '/[0-9]{4}-[0-9]{2}-[0-9]{1,2}/'

The above line gives empty rows

上面的行提供空行。

Also, how do I convert the returned result converted to Date? I'd like to do a date compare on the matching results. I'd like to do something like the following ultimately

另外,如何将返回的结果转换为Date?我想对匹配的结果做一个日期比较。最后,我想做如下的事情

   echo "2016-01-01 11:12:13,234" | nawk '/[0-9]{4}-[0-9]{2}-[0-9]{1,2}/ {if(date($0) > date("2016-01-01 10:00:00,234"){print "Yes";}}'

Thank you.

谢谢你!

2 个解决方案

#1


1  

Keep your regex but add 1 after it. Since 1 always evaluates to true, it performs default operation {print $0}.

保留您的regex,但在它之后添加1。因为1总是计算为true,所以它执行默认操作{print $0}。

awk '/[0-9]{4}-[0-9]{2}-[0-9]{1,2}/1' <<< 1999-05-05

For older versions, you can try:

对于旧版本,您可以尝试:

awk '/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/1' <<< 1999-05-05

Ideone Demo

Ideone演示

http://ideone.com/jyYUrM

http://ideone.com/jyYUrM

#2


1  

nawk is a very old, non-POSIX awk which, among other things, doesn't support RE intervals. Get/use gawk if at all possible, otherwise some other modern awk, but if you're stuck with nawk then use /[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/.

nawk是一个非常古老的非posix awk,除了其他功能外,不支持RE interval。如果可能的话,获取/使用gawk,否则其他一些现代awk,但是如果你被限制在nawk,那么使用/[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9]/。

wrt your followup question - post a new question that includes concise, testable sample input and expected output and a much better description of your requirements for help with that as what you have currently written is unclear and could many one of several things.

总结你的后续问题——发布一个新的问题,包括简洁的、可测试的样本输入和预期的输出,以及对你的需求的更好的描述,因为你目前所写的内容还不清楚,而且可能会有很多事情。

#1


1  

Keep your regex but add 1 after it. Since 1 always evaluates to true, it performs default operation {print $0}.

保留您的regex,但在它之后添加1。因为1总是计算为true,所以它执行默认操作{print $0}。

awk '/[0-9]{4}-[0-9]{2}-[0-9]{1,2}/1' <<< 1999-05-05

For older versions, you can try:

对于旧版本,您可以尝试:

awk '/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/1' <<< 1999-05-05

Ideone Demo

Ideone演示

http://ideone.com/jyYUrM

http://ideone.com/jyYUrM

#2


1  

nawk is a very old, non-POSIX awk which, among other things, doesn't support RE intervals. Get/use gawk if at all possible, otherwise some other modern awk, but if you're stuck with nawk then use /[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/.

nawk是一个非常古老的非posix awk,除了其他功能外,不支持RE interval。如果可能的话,获取/使用gawk,否则其他一些现代awk,但是如果你被限制在nawk,那么使用/[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9]/。

wrt your followup question - post a new question that includes concise, testable sample input and expected output and a much better description of your requirements for help with that as what you have currently written is unclear and could many one of several things.

总结你的后续问题——发布一个新的问题,包括简洁的、可测试的样本输入和预期的输出,以及对你的需求的更好的描述,因为你目前所写的内容还不清楚,而且可能会有很多事情。