Skip to content

Instantly share code, notes, and snippets.

@brenttheisen
Created December 27, 2013 22:16
Show Gist options
  • Save brenttheisen/8153420 to your computer and use it in GitHub Desktop.
Save brenttheisen/8153420 to your computer and use it in GitHub Desktop.
Outputs the contents of the file specified to stdout beginning at the line after the last line it saw.
#!/usr/bin/env bash
if [ "$#" -ne 3 ]; then
echo "Usage: `basename $0` file filtercmd outputcmd"
exit 85
fi
FILE=$1
FILTERCMD=$2
OUTPUTCMD=$3
LAST_LINE_FILE="`dirname $FILE`/.lastline.`basename $FILE`"
CURRENT_LINE=`wc -l < $FILE`
if [ -f "$LAST_LINE_FILE" ]; then
LAST_LINE=$(cat $LAST_LINE_FILE)
if [ "$LAST_LINE" -gt "$CURRENT_LINE" ]; then
LAST_LINE="0"
else
LAST_LINE=$(($LAST_LINE+1))
fi
else
LAST_LINE="0"
fi
OUTPUT=$(tail -n+$LAST_LINE "$FILE")
echo $CURRENT_LINE > $LAST_LINE_FILE
if [ -n "$OUTPUT" ]; then
FILTEREDOUTPUT=$(echo "$OUTPUT" | eval $FILTERCMD)
if [ -n "$FILTEREDOUTPUT" ]; then
echo "$FILTEREDOUTPUT" | eval $OUTPUTCMD
exit $?
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment