bash - Use Awk and Grep to find lines between two time in a log file -


i'm searching log file see if contains string between 2 different time. i.e. if foo exists between lines starts timestamp of 2016-11-10 06:45:00 , 2016-11-10 10:45:00 threshold variable sets time between, example 240 4 hours.

current="$(date "+%y-%m-%d %h:%m:%s")" threshold=240 dt_format="+%y-%m-%d %h:%m:%s" from="$(date -d "$threshold minutes ago" "$dt_format")"  if awk '$0 >= "$from" && $0 <= "$current"' /path/file.log | grep "foo"    exit 0 else    exit 1 fi 

however i'm not sure why when pass $from , $current in command line in if statement, it's not reading it. it's if i'm passing in garbage it's not comparing dates right , return lines , exit 0.

but if manually put in dates in if statement, i.e. 2016-11-10 06:45:00 from , 2016-11-10 10:45:00 current returns correct lines in between 2 dates , can use grep check whether lines contain foo.

i don't understand why code isn't working, , can't manually put in dates need able check between 2 different time based on needs changing threshold variable.

2016-11-10 06:45:00 how timestamp formatted in log, starting in beginning of each lines.

thanks.

you attempting have bash expand variables single quotes... run s="string"; echo '$s' , you'll see mean.

so '$0 >= "$from" && $0 <= "$current"' literally means exact characters. not wanted.

"but that's argument awk"... right awk knows how handle $0 , $1, awk expanding those. expecting awk '$0 >= "some_time" && $0 <= "some_other_time"' didn't!

so, way pass variables awk doing some_variable="world"; awk -v my_variable=$some_variable 'begin{print "hello", my_variable}'

so should have if awk -v f="$from" -v c="$current" '$0 >= f && $0 <= c' /path/file.log | grep "foo"

check out http://www.catonmat.net/blog/ten-awk-tips-tricks-and-pitfalls/ article has insight neat things can awk. might able use "split file on patterns" here reduce amount of commands use either way you'll learn awk.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -