GNU awk :
Script lookup_by_daterange.awk
:
#!/usr/bin/awk -f
function get_ts(dt, ts){
gsub("/"," ",dt);
sub(":"," ", dt);
cmd="date -d \""dt"\" +%s";
while(cmd | getline t) ts=t;
close(cmd);
return ts
}
BEGIN {
from = get_ts(from); # 'from' datetime
to = get_ts(to); # 'to' datetime
}
/^# [0-9]{2}\//{
ts = get_ts($2);
if (ts >= from && ts <= to) {
f=1; n=NR+3
}
}
f && NR<=n {
print
}
NR==n {
print ""
}
Uso:
awk -v from="11/Oct/2017:10:40:29" -v to="11/Oct/2017:10:41:00" -f lookup_by_daterange.awk logfile
A saída:
# 11/Oct/2017:10:40:29 -0400; conn=-1; op=44
dn: cn=schema
changetype: modify
replace: ds-sync-generation-id
# 11/Oct/2017:10:40:30 -0400; conn=-1; op=59
dn: cn=admin data
changetype: modify
replace: ds-sync-state