Created
September 5, 2024 17:32
-
-
Save aidos/5a6a3fa887f41f156b282d72e1b79f43 to your computer and use it in GitHub Desktop.
Quick dirty hack to find the location of a timestamp in a huge log file (assuming logs are roughly in order)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Quick dirty hack to let me find the location of a timestamp in a huge log file | |
file="$1" | |
date="$2" | |
if [ -z "$file" ] || [ -z "$date" ]; then | |
echo 'Invalid arguments! Example use:' | |
echo ' ak_locate_in_log ./path_to_file "2023-08-13 01:00:02.085"' | |
exit 1 | |
fi | |
size=$(stat --format=%s "$file") | |
# work in 1M blocks | |
blocks=$(( $size / (1024 * 1024) )) | |
# skip to the middle and grab a block | |
skip=$(( $blocks / 2 )) | |
range_size=$skip | |
while : ; do | |
first=$(dd status=none if="$file" bs=1M skip=$skip count=1 | egrep -o '^[0-9-]{10} [0-9:]+' | sort | head -n1) | |
last=$(dd status=none if="$file" bs=1M skip=$skip count=1 | egrep -o '^[0-9-]{10} [0-9:]+' | sort | tail -n1) | |
echo first=$first | |
echo last=$last | |
echo date=$date | |
range_size=$(( $range_size / 2 )) | |
if [[ $range_size = 0 ]]; then | |
break | |
fi | |
if [[ "$date" > "$last" ]]; then | |
echo "Going up" | |
skip=$(( "$skip" + "$range_size" )) | |
continue | |
fi | |
if [[ $date < $first ]]; then | |
echo "Going down" | |
skip=$(( $skip - $range_size )) | |
continue | |
fi | |
# done | |
break | |
# TODO what about no first / last? | |
done | |
echo "Stopped at block: $skip" | |
echo | |
echo dd status=none if="$file" bs=1M skip="$skip" count=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment