linux - How can I retrieve logs between two timeframes using regex? -
i have huge log file, each line log entry it's own timestamp. how can retrieve logs in-between 2 specified timestaps (ie. start time - 22:00:00, end time - 23:00:00)?
using bash, possible generate regex statement based on 2 input timestamps, may pipe command (such grep
):
#!/bin/bash #this bare-bone recursive script accepts input of start/end timeframe #in 24-hour format, based on regex statement generated #select values in between 2 timeframes. please note script #has undergone optimization (i apologize reading difficulty, #did not occur me until might have use such script). #you free use, distribute, or modify script heart's content. #constructive feedback welcome. did best eliminate bugs, should #you find case generated regex incorrect timestamps, please #let me know. echo $0 - args passed: $1 $2 start_time=$(echo $1 | tr -d ":") end_time=$(echo $2 | tr -d ":") diff_char="" regex="" function loop () { diffcharvalue=$1 maxval=$2 while [ $diffcharvalue -lt $maxval ] regex="${regex}:[0-5][0-9]" let diffcharvalue+=2 done } function regexgen () { diffchar=$1 start=$2 end=$3 if [ $diffchar -le 6 ]; regexgen $(($diffchar + 1)) $start $end 0 #the fourth arg acts recursion indicaton, whether function called recursively or not let diffchar-=1 diffcharminusone=$(($diffchar - 1)) startbegin=${start:0:$diffcharminusone} endbegin=${end:0:$diffcharminusone} startcut=${start:$diffcharminusone:1} endcut=${end:$diffcharminusone:1} endstartdiff=$(($endcut-$startcut)) if [ $(($diffchar % 2)) -eq 0 ]; if [ $4 -eq 0 ]; regex="${regex}$startbegin[$startcut-9]" loop $diffchar 6 regex="${regex}|$endbegin[0-$endcut]" loop $diffchar 6 regex="${regex}|" elif [ $endstartdiff -gt 1 ]; if [ $endstartdiff -eq 2 ]; regex="${regex}$startbegin[$(($startcut+1))]" else regex="${regex}$startbegin[$(($startcut+1))-$(($endcut-1))]" fi loop $diffchar 6 echo $regex else echo ${regex%?} fi else if [ $4 -eq 0 ]; if [ $startcut -lt 5 ]; regex="${regex}$startbegin[$startcut-5][0-9]" loop $diffchar 5 regex="${regex}|" fi if [ $endcut -gt 0 ]; regex="${regex}$endbegin[0-$endcut][0-9]" loop $diffchar 5 regex="${regex}|" fi elif [ $endstartdiff -gt 1 ]; if [ $diffcharminusone -gt 0 ]; regex="${regex}$startbegin" fi if [ $endstartdiff -eq 2 ]; regex="${regex}[$(($startcut+1))][0-9]" else regex="${regex}[$(($startcut+1))-$(($endcut-1))][0-9]" fi loop $diffchar 5 echo $regex else echo ${regex%?} fi fi fi } in {0..5} if [ ${end_time:$a:1} -gt ${start_time:$a:1} ];then diff_char=$(($a+1)) break fi done result=$(regexgen $diff_char $start_time $end_time 1 | sed 's/\([0-9][0-9]\)/\1:/g') echo $result
Comments
Post a Comment