I have been working on some code for a while, that will parse a log file, look for a specified time discrepancy between entries, and then print that line +/- n other lines out to a file...
#!/bin/bash
file=$1 # The input log file
maxTime=$2 # The time discrepancy to look for
n=$3 # The number of log entries to memorize
outFile=$4 # The output file
declare -a array
if [[ ! -n $1 || ! -n $2 || ! -n $3 || ! -n $4 ]]; then
echo "ERROR"
exit 128
fi
# This value is to print lines AFTER match
till=0
# This is a temp value to compare timestamps
previous=0
# egrep gets rid of all non-timestamped lines
egrep -n '^[0-9]' $file | \
while IFS="[.|:]" read number datetime min sec milli rest; do
milliseconds=$(date -d "$datetime:$min:$sec" +%s)$milli
line="$number:$datetime:$min:$sec.$milli|$rest"
# Memorize the last n lines
array=("${array[@]}" $line)
if [[ ${#array[@]} -gt $n ]] ; then
# get rid of overflow
array=(${array[@]:1})
fi
# Calculate time difference on current and last line
if [[ $previous -ne 0 ]] ; then
(( difference = milliseconds - previous ))
else
difference=0
fi
if [[ $till -gt $number ]] ; then
echo $line
else
if [[ $difference -gt $maxTime ]] ; then
(( till = number + n ))
# Print the history
for item in ${array[@]}
do
echo $item
done
# Print the current match
echo $line
fi
fi
previous=$milliseconds
done
Can somebody tell me what is wrong here?
I think that the problem is that "$line" sometimes contains spaces in its text... which are then interpreted as separate array items... which fills up my buffer with parts of a single line instead of the memorized lines... but honestly I don't know what I can do about that