awk用于日期范围日期格式,例如mm/dd/yyyy hh:mm:ss [英] awk for date ranges date format like mm/dd/yyyy hh:mm:ss

查看:167
本文介绍了awk用于日期范围日期格式,例如mm/dd/yyyy hh:mm:ss的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的日志文件,我正在尝试检索日期范围

I have my log file like this i am trying retrive date range

"07/10/2013 01:31:54","SNMP" 

"07/10/2013 01:31:54","SNMP" 
.... ... .. 
"07/10/2013 03:03:54","SNMP"

我正在使用休闲awk命令,它提供了所有行,我尝试了不同的组合,但没有使用,是否需要在awk中使用标准数据格式?

I am using fallowing awk command, it gives all the rows, i tried different combination no use, is there standard data format need to use in awk?

awk -F, '"07/10/2013 01:35:40" > $1&&$1 <= "07/10/2013 01:50:03"' Mylog.log | wc -l

推荐答案

您有两个问题: CSV解析日期比较.

您可以使用match() CSV解析功能来解决的第一个问题.

The first one you can solve using match(), or a CSV parsing function.

您可以使用正确的日期格式(例如 ISO-8601 ,这是一个令人高兴的副作用,因为它可以按词法比较日期(例如,时区/夏令时更改). 如果您真正使用的是gawk而不是普通的awknawk,则可以使用内置的日期函数mktime()来解析时间戳并返回一个以秒为单位的序数,该序数可以对数字进行数字比较. awk没有本地日期/时间类型,也没有标准的数据/时间库,因此,在这里进行词法或数字比较是最简单的选择.

The second one you can solve by either using a proper date format like ISO-8601, a happy side-effect being that dates (ex timezone/DST changes) can be compared lexically. If you are really using gawk instead of plain awk or nawk you can use the built-in date function mktime() to parse timestamps and return an epoch-second ordinal which allows dates to be compared numerically. awk has no native date/time types, and no standard data/time libraries, so lexical or numeric comparisons are the most straightforward option here.

gawk的最终选择是令人讨厌的骇客:

A final option with gawk is a nasty hack:

/^"07.10.2013 01:35:40"/,/^"07.10.2013 01:50:03"/ {
    # your code here
}

这使用范围表达式来限制在某些行之间匹配.只要时间是单调递增,这就应该适用于您的文件格式–对于Apache日志,不是正确(因为它们是按完成顺序记录的,但默认情况下包含原始请求时间戳,并不能保证其是单调递增的.

This uses a range expression to limit the scope of matching to between certain lines. This should work for your file format as long as the times are monotonic increasing – this is not true for Apache logs (since they are logged in order of completion, but by default contain the original request time stamp, and are not guaranteed to be monotonic increasing).

这篇关于awk用于日期范围日期格式,例如mm/dd/yyyy hh:mm:ss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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