#!/bin/sh -

# timegrep - grep lines with a Unix time tab column within the given date range
# Steve Kinzler, steve@kinzler.com, Jan 03
# https://kinzler.com/me/home.html#unix

case "$1" in
-h)	set x; shift;;
esac

case "$#" in
1)	set x "$@" zero now; shift;;
2)	set x "$@"      now; shift;;
3)	;;
*)	cat <<EOF 1>&2
usage: $0 column [ start_date | zero | now ] [ end_date | now ]
The column is numbered from one, or is NF to indicate the last column.
By default, the start date is "zero", ie the beginning of the Unix epoch
(1970 Jan 1), and the end date is "now".  Dates may be in either Unix or
English time format.
EOF
	exit 1;;
esac

col="$1"; shift
now=`getdate`

case "$1" in
zero)	start=0;;
now)	start="$now";;
*)	start=`getdate "$1"`;;
esac

case "$2" in
now)	end="$now";;
*)	end=`getdate "$2"`;;
esac

#echo "$0: grepping $start <= column $col <= $end" 1>&2

exec awk -F'	' "\$$col >= $start && \$$col <= $end"
