#!/bin/sh
#
# hssaf - search the Safari history file for a regex pattern
#
# Output format: Amazon.com: Welcome (2004-04-28 09:58:35)
# http://www.amazon.com/
#
SAFHIST="$HOME/Library/Safari/History.plist"
if [ "$1" ]
then
gawk 'BEGIN { FS = "\n"; RS = "\t\t</?dict>" }
{ split($3, url, "</?string>")
split($7, title, "</?string>")
if (url[2] ~ search || title[2] ~ search) {
split($5, epochdate, "</?string>")
date = epochdate[2] + 978307200
print "\n" url[2] "\n" title[2], "(" strftime("%Y-%m-%d %T", date) ")"
}
}' search="$1" "$SAFHIST" | tail -r | sed '$d'
else
echo "Usage: "$(basename "$0")" <pattern>"
fi
Create the script somewhere on your path, make it executable (chmod 755 hssaf), and then use it with the term to be found on the command line: hssaf cnn, for instance.
[robg adds: This script relies on gawk, which isn't included in OS X. It's available via fink, or you can compile it from source by downloading the latest version from the gnu.org FTP servers. It should compile cleanly with a simple ./configure, make, and sudo make install, though I used fink so I can't say for certain.]

