Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Easily search Safari web history files UNIX
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.]
    •    
  • Currently 2.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[11,313 views]  

Easily search Safari web history files | 10 comments | Create New Account
Click here to return to the 'Easily search Safari web history files' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Easily search Safari web history files
Authored by: ajp on Apr 30, '04 12:53:40PM

has anyone tried this with awk instead of gawk

awk comes with os x



[ Reply to This | # ]
Easily search Safari web history files
Authored by: ceesaxp on Apr 30, '04 01:30:01PM

This script (seems to) use a few GNU extensions to awk (hence gawk) -- replacing gawk with awk in it produces neither an error nor any results for known patterns.

I'm sure it can be slightly tweaked to use one true awk, as it comes with OSX.



[ Reply to This | # ]
Easily search Safari web history files
Authored by: ceesaxp on Apr 30, '04 03:30:38PM
ok, i tried to fiddle with it a bit -- you can't just replace gawk with awk. most obvious problem is strftime function which is not included in awk. also, pattern matching for FS, RS and within split does not seem to produce what's expected.

[ Reply to This | # ]
Easily search Safari web history files
Authored by: Han Solo on Apr 30, '04 02:56:07PM

FWIW, the latest beta of LaunchBar also allows searching in Safari (and a few other browsers') web history files.



[ Reply to This | # ]
Easily search Safari web history files
Authored by: ceesaxp on Apr 30, '04 03:31:53PM

and so does quicksilver



[ Reply to This | # ]
Am I missing something?
Authored by: houplagrundle on Apr 30, '04 03:19:32PM

I just open the History tab in the bookmarks window, click the triangles next to the day so I can see all the sites and do a search with command-F like searching a normal web page.



[ Reply to This | # ]
Am I missing something?
Authored by: readparse on Apr 30, '04 04:54:45PM

Yes, I use Command-F to search the history also, but you don't even have to click the triangle. Command-F searches the entire history file and expands as it goes. The only problem is that you have to clean after it because it doesn't collapse the arrows.


The coolest thing about this is that it takes regular expressions, which command-F doesn't.

Incidentally, you can also use it to search your bookmarks, by changing the file to Bookmarks.plist. You could easily modify this script to support either or both.

RP



[ Reply to This | # ]
What about Cmd-F
Authored by: beelers on Apr 30, '04 05:29:49PM

Simply opening the history in Safari and using the find command [Cmd-F] works for me. I would rather do that than entering a shell script and launching Terminal.app every time I want to find an address.

Maybe this is just for the geek who wants to Think Different, which is not always a bad thing.

---
--
no sig is here



[ Reply to This | # ]
Easily search Safari web history files
Authored by: elmimmo on May 01, '04 04:57:47AM

In this field, Internet Explorer for Windows still beats Safari hands down. The Search feature in its History sidebar searches for the entered term in the urls of the visited pages AND in their full cached body (if they are still in the cache). That way you can find already visited pages by what you remember of their content even if you do not remember anything about their name or url.



[ Reply to This | # ]
Easily search Safari web history files
Authored by: kap on May 02, '04 10:41:58PM

I've got the fink package coreutils installed on my system, which installs another version of tail, among other things.

The -r flag is not an option in the coreutils program, but it is in the OS X native binary. I ended up hardcoding the path in this script to the /usr/bin/tail binary, and it works fine afterwards.

Great script.



[ Reply to This | # ]