case $# in
3) ;;	# ok
*) echo "Usage: S history nmatched pattern file" 1>&2; exit 1;;
esac
file=$3
if test ! -r $file
	then echo "Can not read audit file $file; no history" 1>&2; exit 1
	fi
$SHOME/cmd/reverse $file |awk "
BEGIN {
	state=1
	max = $1; n = 0
	matched = 0
	}
state == 1 {
	if(\$0 ~ /^#~/) state = 2
	continue
	}
\$0 !~ /^#~/ && /$2/{"'
	matched = 1
	}
state == 2 && $0 !~ /^#~/ {
	state=3
	expr=$0
	continue
	}
state == 3 && $0 !~ /^#~/ {
	expr=$0 "\n" expr
	continue
	}
state == 3 && $0 ~ /^#~/ {
	if(matched && expr !~ /history *\(/ && expr !~ /^.Random.seed <-/ && expr !~ /again *\(/) {
		print expr
		print "#~"
		if(++n >= max)exit(0)
		}
	state = 2
	matched = 0
	}
'
# state 1 -- scanning for first line with #~
# state 2 -- found #~, now looking for first line without #~
# state 3 -- found first line of statement
