Easily search Safari web history files

Apr 30, '04 09:11:00AM

Contributed by: ChrisR

Here's a quick and dirty script to locate your web browser history files and search through the addresses in them. I came up with this when a user of told me about a website they had visited but couldn't remember the exact address. Rather than waste time trolling through history files and search engine results, I wrote this code. Comments and suggestions are more than welcome.

#!/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.]

Comments (10)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20031013210227110