#!/usr/bin/perl # searchbib.pl # # seach Bib utility using a hash # 2000-11-08: created # 2005-09-05: modified for new directory structure # # Bib # / \ # Scripts Data # # 2005-09-08: better regexp, multiple file search # # ToDo: search by multiple keys # # Mafuyu Kitahara # if something goes wrong, use the following... # grep -B 7 "keywd = .*xxx" * > xxx.out ## Front end @bibfiles = `ls ../Data/*.bib`; @outfiles = `ls ../Data/*.out`; @files = (@bibfiles,@outfiles); # to ensure that bibfiles always come first print "Bib and previous search results in data directory are: \n"; $entnumber=0; foreach $entry (@files) { print "$entnumber: $entry"; $entnumber++; } print "-----------\n"; print "Search in which file? Enter the number (or * for all files)"; chomp($num = ); if ($num =~/\d/) { @files = $files[$num]; print "Search in @files\n"; } ### needs more robust error check!!! print "Type x=pattern where 'x' can be a:Author t:Title k:Keyword y:year b:booktitle f:form (letter, A4, paper-bound...etc) n:note j:journal and 'pattern' is what you are looking for Example: a=Mafuyu >"; chop($pattern = ); @key = split ('=', $pattern); if ($key[0]=~/a/) {$key = "author";} elsif ($key[0]=~/t/) {$key = "title";} elsif ($key[0]=~/k/) {$key = "keywd";} elsif ($key[0]=~/y/) {$key = "year";} elsif ($key[0]=~/f/) {$key = "form";} elsif ($key[0]=~/j/) {$key = "journal";} elsif ($key[0]=~/b/) {$key = "booktitle";} elsif ($key[0]=~/n/) {$key = "note";} else { die "Please enter `x=' before search pattern! \n"; } print "Search for `$key[1]' in `$key' field\n"; open (OUT, ">>../Data/$key[1].out"); ## search and print loop for $file (@files) { open (DATA, "$file"); $/ = ""; $* = 1; while () { %record = (); for(split(/\n/)) { # set a scope to $head and $val, then take records into them my ($head, $val) = /\s+(\w+)\s+\=\s+\{*(.*)\}*\,*$/; # remove final "}," $val =~ s/\}*\,*$//; # print "head=$head val=$val \n"; # debug # store in a hash, $head as key, $val as value $record{$head} = $val; } if ($record{$key}=~/$key[1]/) { print; print OUT; } } }