📄 ch14_12.htm
字号:
<HTML><HEAD><TITLE>Recipe 14.11. Program: ggh - Grep Netscape Global History (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:43:02Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch14_01.htm"TITLE="14. Database Access"><LINKREL="prev"HREF="ch14_11.htm"TITLE="14.10. Executing an SQL Command Using DBI and DBD"><LINKREL="next"HREF="ch15_01.htm"TITLE="15. User Interfaces"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch14_11.htm"TITLE="14.10. Executing an SQL Command Using DBI and DBD"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 14.10. Executing an SQL Command Using DBI and DBD"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch14_01.htm"TITLE="14. Database Access"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="chapter"HREF="ch15_01.htm"TITLE="15. User Interfaces"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15. User Interfaces"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch14-94911">14.11. Program: ggh - Grep Netscape Global History</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch14-idx-1000005014-0"></A><ACLASS="indexterm"NAME="ch14-idx-1000005014-1"></A><ACLASS="indexterm"NAME="ch14-idx-1000005014-2"></A><ACLASS="indexterm"NAME="ch14-idx-1000005014-3"></A><ACLASS="indexterm"NAME="ch14-idx-1000005014-4"></A>This program divulges the contents of Netscape's <EMCLASS="emphasis">history.db</EM> file. It can be called with full URLs or with a (single) pattern. If called without arguments, it displays every entry in the history file. The <EMCLASS="emphasis">~/.netscape/history.db</EM> file is used unless the <BCLASS="emphasis.bold">-database</B> option is given.</P><PCLASS="para">Each output line shows the URL and its access time. The time is converted into <CODECLASS="literal">localtime</CODE> representation with <BCLASS="emphasis.bold">-localtime</B> (the default), <CODECLASS="literal">gmtime</CODE> representation with <BCLASS="emphasis.bold">-gmtime </B> - or left in raw form with <BCLASS="emphasis.bold">-epochtime</B>, which is useful for sorting by date.</P><PCLASS="para">To specify a pattern to match against, give one single argument without a <CODECLASS="literal">://</CODE>.</P><PCLASS="para">To look up one or more URLs, supply them as arguments:</P><PRECLASS="programlisting">% ggh http://www.perl.com/index.html</PRE><PCLASS="para">To find out a link you don't quite recall, use a regular expression (a single argument without a <CODECLASS="literal">://</CODE> is a pattern):</P><PRECLASS="programlisting">% ggh perl</PRE><PCLASS="para">To find out all the people you've mailed:</P><PRECLASS="programlisting">% ggh mailto:</PRE><PCLASS="para">To find out the FAQ sites you've visited, use a snazzy Perl pattern with an embedded <CODECLASS="literal">/i</CODE> modifier:</P><PRECLASS="programlisting">% ggh -regexp '(?i)\bfaq\b'</PRE><PCLASS="para">If you don't want the internal date converted to localtime, use <BCLASS="emphasis.bold">-epoch</B>:</P><PRECLASS="programlisting">% ggh -epoch http://www.perl.com/perl/</PRE><PCLASS="para">If you prefer gmtime to localtime, use <BCLASS="emphasis.bold">-gmtime</B>:</P><PRECLASS="programlisting">% ggh -gmtime http://www.perl.com/perl/</PRE><PCLASS="para">To look at the whole file, give no arguments (but perhaps redirect to a pager):</P><PRECLASS="programlisting">% ggh | less</PRE><PCLASS="para">If you want the output sorted by date, use the <BCLASS="emphasis.bold">-epoch</B> flag:</P><PRECLASS="programlisting">% ggh -epoch | sort -rn | less</PRE><PCLASS="para">If you want it sorted by date into your local time zone format, use a more sophisticated pipeline:</P><PRECLASS="programlisting">% ggh -epoch | sort -rn | perl -pe 's/\d+/localtime $&/e' | less</PRE><PCLASS="para">The Netscape release notes claim that they're using NDBM format. This is misleading: they're actually using Berkeley DB format, which is why we require DB_File (not supplied standard with all systems Perl runs on) instead of NDBM_File (which is). The program is shown in <ACLASS="xref"HREF="ch14_12.htm#ch14-14152"TITLE="ggh">Example 14.8</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch14-14152">Example 14.8: ggh</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# ggh -- grovel global history in netscape logs$USAGE = <<EO_COMPLAINT;usage: $0 [-database dbfilename] [-help] [-epochtime | -localtime | -gmtime] [ [-regexp] pattern] | href ... ]EO_COMPLAINTuse Getopt::Long;($opt_database, $opt_epochtime, $opt_localtime, $opt_gmtime, $opt_regexp, $opt_help, $pattern, ) = (0) x 7;usage() unless GetOptions qw{ database=s regexp=s epochtime localtime gmtime help };if ($opt_help) { print $USAGE; exit; }usage("only one of localtime, gmtime, and epochtime allowed") if $opt_localtime + $opt_gmtime + $opt_epochtime > 1;if ( $opt_regexp ) { $pattern = $opt_regexp;} elsif (@ARGV && $ARGV[0] !~ m(://)) { $pattern = shift;}usage("can't mix URLs and explicit patterns") if $pattern && @ARGV;if ($pattern && !eval { '' =~ /$pattern/; 1 } ) { $@ =~ s/ at \w+ line \d+\.//; die "$0: bad pattern $@";}require DB_File; DB_File->import(); # delay loading until runtime$| = 1; # feed the hungry PAGERs$dotdir = $ENV{HOME} || $ENV{LOGNAME};$HISTORY = $opt_database || "$dotdir/.netscape/history.db";die "no netscape history dbase in $HISTORY: $!" unless -e $HISTORY;die "can't dbmopen $HISTORY: $!" unless dbmopen %hist_db, $HISTORY, 0666;# the next line is a hack because the C programmers who did this# didn't understand strlen vs strlen+1. jwz told me so. :-)$add_nulls = (ord(substr(each %hist_db, -1)) == 0);# XXX: should now do scalar keys to reset but don't # want cost of full traverse, required on tied hashes.# better to close and reopen?$nulled_href = ""; $byte_order = "V"; # PC people don't grok "N" (network order) if (@ARGV) { foreach $href (@ARGV) { $nulled_href = $href . ($add_nulls && "\0"); unless ($binary_time = $hist_db{$nulled_href}) { warn "$0: No history entry for HREF $href\n"; next; } $epoch_secs = unpack($byte_order, $binary_time); $stardate = $opt_epochtime ? $epoch_secs : $opt_gmtime ? gmtime $epoch_secs : localtime $epoch_secs; print "$stardate $href\n"; }} else { while ( ($href, $binary_time) = each %hist_db ) { chop $href if $add_nulls; next unless defined $href && defined $binary_time; # gnat reports some binary times are missing $binary_time = pack($byte_order, 0) unless $binary_time; $epoch_secs = unpack($byte_order, $binary_time); $stardate = $opt_epochtime ? $epoch_secs : $opt_gmtime ? gmtime $epoch_secs : localtime $epoch_secs; print "$stardate $href\n" unless $pattern && $href !~ /$pattern/o; }}sub usage { print STDERR "@_\n" if @_; die $USAGE;}<ACLASS="indexterm"NAME="ch14-idx-1000005016-0"></A><ACLASS="indexterm"NAME="ch14-idx-1000005016-1"></A><ACLASS="indexterm"NAME="ch14-idx-1000005016-2"></A><ACLASS="indexterm"NAME="ch14-idx-1000005016-3"></A><ACLASS="indexterm"NAME="ch14-idx-1000005016-4"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-1620">See Also</A></H3><PCLASS="para"><ACLASS="xref"HREF="ch06_18.htm"TITLE="Expressing AND, OR, and NOT in a Single Pattern">Recipe 6.17</A><ACLASS="indexterm"NAME="ch14-idx-1000004909-0"></A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch14_11.htm"TITLE="14.10. Executing an SQL Command Using DBI and DBD"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 14.10. Executing an SQL Command Using DBI and DBD"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="chapter"HREF="ch15_01.htm"TITLE="15. User Interfaces"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15. User Interfaces"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">14.10. Executing an SQL Command Using DBI and DBD</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">15. User Interfaces</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -