Skip to content

Instantly share code, notes, and snippets.

@spudtrooper
Created March 17, 2012 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spudtrooper/2062394 to your computer and use it in GitHub Desktop.
Save spudtrooper/2062394 to your computer and use it in GitHub Desktop.
Monitors the file you pass in, printing the number of lines. You pass in the optional file as the first parameter (defaults to most recently updated file) and optional sleep time in seconds as the second argument.
#!/bin/sh
#
# Monitors the file you pass in, printing the number of lines. You
# pass in the optional file as the first parameter (defaults to most
# recently updated file) and optional sleep time in seconds as the
# second argument.
#
# Examples:
#
# % ./monitor # -- use the most recently updated file
#
# % ./monitor some-file.txt 1 # -- updates every 1 second
#
# % ./monitor some-file.txt # -- updates every 5 seconds
# some-file.txt : 1496
# some-file.txt : 1501 [1 line(s)/sec]
# some-file.txt : 1519 [3 line(s)/sec]
#
#
file=$1
shift
SLEEP=${1-5}
shift
if [ "$file" == "" ]; then
file=$(ls -l | awk '{print $(NF-1) " " $NF}' | \
sort -r | grep -v "^total" | head -1 | awk '{print $2}')
echo "Using $file"
fi
last=-1
while [ 1 ]; do
num=$(wc -l $file | awk '{print $1}')
if [ "$last" != "-1" ]; then
lines_per_sec=$(( ($num-$last)/$SLEEP ))
msg="$num [${lines_per_sec} line(s)/sec]"
else
msg=$num
fi
last=$num
echo "$file : $msg"
sleep $SLEEP
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment