Last active
May 22, 2020 14:23
-
-
Save mrbald/b54fce0310529e9d51478793e7d7b334 to your computer and use it in GitHub Desktop.
A script to incrementally poll a log file of timestamp-prefixed text records
This file contains hidden or 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 | |
# | |
# A script to incrementally poll a log file. | |
# Subsequent invocations print lines added since the previous invocation. | |
# An assumption is that log lines begin with a timestamp 23 characters long (see/tweak the TS_LEN variable below). | |
# | |
# The state is stored in the file <original_filename>.seen as a last_line_no + last_timestamp pair last seen. | |
# If the state file is not there, or the line_no in the target file has the last_timestamp | |
# different from the one in the state file, then the file is printed from the line 1. | |
# | |
set -eu | |
trap 'echo "bye"' EXIT | |
echo "hi" | |
declare -i TS_LEN=23 | |
declare -r LOG_FILE=$1 | |
declare -r POS_FILE="${LOG_FILE}.seen" | |
if [[ -r ${LOG_FILE} ]]; then | |
read -r -n${TS_LEN} log_stamp < "${LOG_FILE}" | |
declare -r last_line=$(echo -n $(wc -l < "${LOG_FILE}")) | |
first_line=1 | |
if [[ -s ${POS_FILE} ]]; then | |
read seen_line seen_stamp < "${POS_FILE}" | |
if [[ $log_stamp == $seen_stamp ]]; then | |
first_line=$(( seen_line + 1 )) | |
fi | |
fi | |
echo "${last_line} ${log_stamp}" > "${POS_FILE}" | |
sed -n "${first_line},${last_line}p" "${LOG_FILE}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment