Skip to content

Instantly share code, notes, and snippets.

@bcchenbc
Created October 3, 2018 07:13
Show Gist options
  • Save bcchenbc/71179e34d6b81d477b94b7e3c5988f8d to your computer and use it in GitHub Desktop.
Save bcchenbc/71179e34d6b81d477b94b7e3c5988f8d to your computer and use it in GitHub Desktop.
AWK script for counting log lines that were generated in a specific time period
#!/usr/bin/awk -f
function MonInNum(in_str) {
return (index("JanFebMarAprMayJunJulAugSepOctNovDec",in_str)+2)/3
}
function TimeMaker(year, mon, day, hour, min, sec, utc) {
datestring = year" "mon" "day" "hour" "min" "sec" "utc
return mktime(datestring)
}
BEGIN {
time_format = "%b %e %H:%M:%S %Z %Y"
start_time = systime() - 33*60*60
end_time = systime() - 14*60*60
printf("Printing lines logged \n after %s (%s) and \n before %s (%s):\n",
strftime(time_format, start_time), start_time,
strftime(time_format, end_time), end_time)
count = 0
}
{
hour = substr($3,1,2)
min = substr($3,4,2)
sec = substr($3,7,2)
timestamp = TimeMaker(2018, MonInNum($1), $2, hour, min, sec, 0)
#print strftime(time_format, timestamp)
if (timestamp > start_time && timestamp < end_time) {
print($0)
count += 1
}
}
END {
print("Line count: " count)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment