Command line tool to extract .bib entry by key?

Disclaimer: I'm not sure if this is the droid tool you are looking for. :) I wrote it in my lunch time, so it might be very buggy. But we do love problematic software, don't we? :)

Introducing fred1:

Fred 1

The idea here is to behave exactly Seamus' idea: given a bibfile and a key, return the desired entry, if any. fred is written in Java, but here I created a shellscript file to call it for me:

#!/bin/bash

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd -P "$( dirname "$SOURCE" )" && pwd )"
java -jar "$DIR/fred.jar" $*

Credits to Joseph Wright for this awesome script.

The usual call to fred would be:

$ java -jar fred.jar <bibfile> <key>

Consider the following .bib file:

@MISC{osi:1998,
  author = {Bruce Perens and Eric Steven Raymond},
  title = {Open Source Initiative},
  year = {1998},
  note = {Nonprofit corporation with global scope formed to educate about and
    advocate for the benefits of open source and to build bridges among
    different constituencies in the open source community.},
  url = {http://www.opensource.org/}
}

@Article{tversky83,
  author =   {Amos Tversky and Daniel Kahneman},
  title =    {Extension versus intuitive reasoning: The conjunction fallacy
             in probability judgement},
  journal =  {Psychological Review},
  year =     1983,
  volume =   90,
  pages =    {293--315}
}

Now let's simply call fred on my references.bib file:

Fred 2

How did I set the output format? fred uses a local (not global) configuration file written in the YAML format which determines how the output should look like. Let's see my sample config.yaml file:

types:

- type: MISC
  format: '$!{author}. $!{title}.'

- type: ARTICLE
  format: '$!{author}. "$!{title}". $!{journal} ${volume} (${year}): ${pages}.'

type is related to, well, the type of the BibTeX entry (article, book, technical report, and so forth), while format holds the template used to print the entry. The template is written using the Apache Velocity template engine. I could add some more complex verifications (e.g, if the field is empty, suppress the next punctuation symbol), but the template would become quite complex for a simple example. The only "advanced" part of this template is the use of the "silent" fallback for variables, that is, the ${author} variable will be resolved to the author field of the BibTeX entry, and stay as it is if the field is absent. If I use $!{author}, an empty variable is not displayed.

Let's now run fred again:

Fred

It's important to observe that the author field is treated as it is. I would have to include a static method for parsing each author and display their names accordingly. I'll try to write some helper methods for this feature in the near future, but for now, it's what I can offer - after all, I wrote it in my lunch time. :)

If we try to print an invalid key:

Fred 4

fred is available in its own GitHub repository, under the Downloads section. Note that fred requires the Java Virtual Machine, at least from version 1.5 on. The shellscript file is also provided, make sure to include both fred and fred.jar files at the same folder, and add it to the system path.

Hope you guys like this humble tool. :)


  1. fred was formely known as horned owl (blame percusse). :) An exercise for the reader: find out what fred means. Hint: ask Andrew Stacey. :)

Here is a Perl program template that can be used to extract any parts of the entry based on the key:

#! /usr/bin/env perl

use warnings;
use strict;
use feature qw(say);
use Text::BibTeX;

die "Usage $0 <bibfile> <key>\n" if @ARGV != 2;
my $fn=shift;
my $key=shift;

my $bibfile = new Text::BibTeX::File($fn);

while (my $entry = new Text::BibTeX::Entry($bibfile)) {
  if (!$entry->parse_ok) {
    warn "error in input";
    next;
  }
  my $ckey = $entry->key; 
  if (defined $ckey) {
    my $title="<no title found>";
    if ($ckey eq $key) {
      if ($entry->exists ('title')) {
        $title = $entry->get ('title');
      }
      say "$key : '$title'";
      exit;
    }
  }
}
say "Key '$key' was not found.";

We here extract the title of the entry with the given key as an example. (Note: You must have installed the libbtparse-dev package on Ubuntu... that is run: sudo apt-get install libbtparse-dev)

Tags:

Bibtex

Tools