(This is an answer)
Here's a script that I used to use for managing BibTeX .bib files before I learnt how to Do Things Properly! The only bit it doesn't do is extract the references from the .aux file. But that wouldn't be very hard to do. The vast majority of this script is about setting up the configuration. The actual meat of it is in the last loop which slurps through a .bib file and looks for the appropriate entries. The reason that that loop is so short is because someone else has already written a Text::BibTeX perl module, whereupon the rest is window dressing. If this is the sort of thing that you are after, then it would be really easy to adapt it to (a) search through the .aux file to find the references and (b) not bother with all the other things that I used it to do (such as automatically download references from the arXiv).
#! /usr/bin/perl -w
use strict;
#use Getopt::Long qw(:config auto_help bundling);
use Getopt::Long qw(:config bundling);
use Pod::Usage;
use Text::BibTeX;
use LWP::MediaTypes qw(guess_media_type);
# This is the only variables that should need customising
my $conffile = "$ENV{HOME}/.refsrc";
###
# Nothing below here should need customisation
###
my (
$bibbase,
$hostname,
$docbase,
$reffile,
$lynx,
$arxivdir,
$ext,
$authors,
$refs,
$reduced,
$silent,
$titles,
$append,
$view,
$show,
$help,
$man,
$gpl,
$bibfile,
$entry,
$output
);
my @reffiles;
my %mime;
my %values;
my @shrt;
my %tests;
@shrt = ("title","author");
%tests = (
"authors" => [0, sub {
my ($e,$b) = @_;
my @a;
if ($$e->exists('author')) {
@a = $$e->split('author');
} elsif ($$e->exists('editor')) {
@a = $$e->split('editor');
} else {
return 0;
}
for (my $j = 0; $j <= $#$b; $j++) {
for (my $i = 0; $i <= $#a; $i++) {
return 1 if $a[$i] =~ /$$b[$j]/i;
}
}
return 0;
}],
"refs" => [0, sub {
my ($e,$r) = @_;
for (my $i = 0; $i <= $#$r; $i++) {
return 1 if ($$e->key =~ /^$$r[$i]$/);
}
return 0;
}],
"titles" => [0, sub {
my ($e,$t) = @_;
for (my $i = 0; $i <= $#$t; $i++) {
return 1 if ($$e->get('title') =~ /$$t[$i]/i);
}
return 0;
}]
);
# Default display routine
$output = sub {
my ($e) = @_;
if ($append) {
$$e->set_key($append . $$e->key);
}
$$e->print();
};
GetOptions (
# List of things to look for in the author/editor entries
"a|authors=s@" => sub {
my ($a, $b) = @_;
push @{$values{"authors"}}, split(",",$b);
$tests{"authors"}[0] = 1;
},
# List of things to look for in the reference entries, array?
"e|refs=s@" => sub {
my ($a, $b) = @_;
push @{$values{"refs"}}, split(",",$b);
$tests{"refs"}[0] = 1;
},
# Whether to print the full entry or a basic summary
"r|reduced" => sub {
$output = sub {
my ($e) = @_;
print $append if ($append);
print $$e->key . "\n";
for (my $i = 0; $i <= $#shrt; $i++) {
$$e->exists("$shrt[$i]") and print $$e->get("$shrt[$i]") and print "\n";
}
}
},
# Whether to produce any output, or just an exit code
"s|silent" => sub {
$output = sub {
exit 1;
}
},
# List of things to look for in the title entries
"t|title=s@" => sub {
my ($a, $b) = @_;
push @{$values{"titles"}}, split(",",$b);
$tests{"titles"}[0] = 1;
},
# Modify references by appending this string
"u|append=s" => \$append,
# Try to display an appropriate file
"v|view" => sub {
$output = sub {
my ($e) = @_;
my ($mi,$ex);
# if the key has three digits, then it's highly likely to be an arXiv ref
my $u = ($entry->key =~ /\d\d\d/ ? "" : chr(95));
my @p = glob("$docbase/*/" . $entry->key . "$u*");
if (!@p) {
print "Couldn't find a file for: " . $entry->key . "\n";
}
for (my $i = 0; $i <= $#p; $i++) {
$mi = guess_media_type($p[$i]);
if (exists($mime{$mi})) {
($ex = $mime{$mi}) =~ s/%s/$p[$i]/;
if (!fork) {
exec $ex
or die "Couldn't execute $ex";
}
} else {
print "Couldn't find a program to view: " . $entry->key . " file: $p[$i] mimetype: $mi\n";
}
}
}
},
# Try to locate an appropriate file
"x|locate" => sub {
$output = sub {
my ($e) = @_;
# if the key has three digits, then it's highly likely to be an arXiv ref
my $u = ($entry->key =~ /\d\d\d/ ? "" : chr(95));
my @p = glob("$docbase/*/" . $entry->key . "$u*");
if (!@p) {
print "Couldn't find a file for: " . $entry->key . "\n";
}
for (my $i = 0; $i <= $#p; $i++) {
print $p[$i];
}
}
},
"h|?|help" => \$help,
"m|man" => \$man,
"gpl" => \$gpl
) or pod2usage(2);
pod2usage(-exitval => 1, -verbose => 0) if $help;
pod2usage(-verbose => 2 ) if $man;
exec 'perldoc perlgpl' if $gpl;
if (!-e $conffile) {
print STDERR "$conffile does not exist.\n";
exit 1;
}
open (CONF, $conffile)
or die "Couldn't open $conffile for reading.\n";
while (<CONF>) {
# Set configuration variables; currently only bibbase and docbase
if (/=/) {
/(\w*)\s*=\s*(.*?)\s*$/;
my $s = "\$$1 = \"$2\"";
eval($s);
$@ and die $@;
next;
}
# Stuff of the form 'name.bib' is a bibtex file to parse
if (/(\w*\.bib)/) {
push @reffiles, $1;
next;
}
# Stuff of the form 'mimetype; application' tells us how to view
# certain files
if (/^\s*([a-z]*\/[a-z-]*);\s*(.*)/) {
$mime{$1} = $2;
}
}
die "$bibbase is not a directory.\n" unless -d $bibbase;
die "$docbase is not a directory.\n" unless -d $docbase;
# Loop over the bibtex reference files looking for the entries
for (my $i = 0; $i <= $#reffiles; $i++) {
if (! -f "$bibbase/$reffiles[$i]" ) {
print STDERR "$reffiles[$i] is not a regular file.\n";
next;
}
$bibfile = new Text::BibTeX::File "$bibbase/$reffiles[$i]";
ENTRY: while ($entry = new Text::BibTeX::Entry $bibfile) {
if (!$entry->parse_ok) {
print STDERR "Error in input, skipping entry\n";
next;
}
next unless ($entry->metatype eq BTE_REGULAR);
foreach my $t (keys %tests) {
next unless $tests{$t}[0];
if (&{$tests{$t}[1]}(\$entry,$values{$t})) {
&$output(\$entry);
next ENTRY;
}
}
}
$bibfile->close;
}
Configuration file example:
bibbase = /home/astacey/texmf/bibtex/bib
docbase = /home/astacey/docs/MathsPapers
hostname = front.math.ucdavis.edu
reffile = /home/astacey/texmf/bibtex/bib/arxiv.bib
lynx = /bin/lynx
arxivdir = /home/astacey/docs/MathsPapers/arXiv/
ext = .pdf
arxiv arxiv.bib
article articles.bib
book books.bib
other misc.bib
application/pdf; /usr/bin/xpdf %s
application/postscript; /usr/bin/gv %s
application/x-dvi; /usr/bin/xdvi %s
book -> book
article -> article
booklet -> book
conference -> book
inbook -> article
incollection -> article
inproceedings -> article
manual -> book
mastersthesis -> other
misc -> other
phdthesis -> other
proceedings -> book
techreport -> article
unpublished -> other
bibtools. – Dirk Eddelbuettel Dec 04 '10 at 19:49bibtoolsmight be the first one I try also. – Jeromy Anglim Dec 06 '10 at 01:16