now, i have to search for a pattern within a particular time frame which the user will provide in the following format:
19/Jun/2018:07:04,21/Jun/2018:21:30
it is easy to get tempted to attempt this search with a variation of the following awk command:
awk '/19\/Jun\/2018:07:04/,/21\/Jun\/2018:21:30/' datafile
but i think it can be more efficient than that. can i do this without having to call external utilities like awk?
im writing this code in /bin/sh. i intend to use this script on different unix flavors which includes but certainly isnt limited to ubuntu, redhat, fedora, debian.
here is the code im using to try to get the time from the log that is the closest to the time specified by the user:
echo "${AllEpochTimes}" | awk -F"," -v c=1 -v t=${GoBackTimeEndin} '{a[NR]=$c}END{
asort(a);d=a[NR]-t;d=d<0?-d:d;v = a[NR]
for(i=NR-1;i>=1;i--){
m=a-t;m=m<0?-m:m
if(m<d){
d=m;v=a
}
}
print v
}'
AllEpochTimes - contains all the converted time stamps of the log. as you can imagine, if the log file is huge, the content of this variable will skyrocket in size.