Script to extract selected entries from a bibtex file
I would recommend using a language with a battle-tested BibTeX library instead of reinventing that wheel. For example
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
use BibTeX::Parser;
open my $fh, '<', $ARGV[0];
my $parser = BibTeX::Parser->new($fh);
my @authoryear;
while (my $entry = $parser->next) {
if ($entry->parse_ok) {
if ($entry->key eq "AuthorYear") {
push @authoryear, $entry;
}
}
else {
warn "Error parsing file: " . $entry->error;
}
}
# I'm not familiar with bibtex files, so this may be insufficient
open my $out, '>', "authoryear.bib";
foreach my $entry (@authoryear) {
say $out $entry->raw_bibtex;
}
You'll probably have to install the module: cpan install BibTeX::Parser
The following Python script does the desired filtering.
#!/usr/bin/python
import re
# Bibliography entries to retrieve
# Multiple pattern compilation from: http://stackoverflow.com/a/11693340/147021
pattern_strings = ['Author2010', 'Author2012',]
pattern_string = '|'.join(pattern_strings)
patterns = re.compile(pattern_string)
with open('bibliography.bib', 'r') as bib_file:
keep_printing = False
for line in bib_file:
if patterns.findall(line):
# Beginning of an entry
keep_printing = True
if line.strip() == '}':
if keep_printing:
print line
# End of an entry -- should be the one which began earlier
keep_printing = False
if keep_printing:
# The intermediate lines
print line,
Personally, I prefer moving to a scripting language when the filtering logic becomes complex. That, perhaps, has an advantage on the readability factor at least.