Command line tool to extract .bib entry by key?

I'd like a tool that, when given a .bib file and a key from that file as inputs, outputs a plain text rendition of the full entry. For example:

bib.bib

@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}}


CLI

me@computer > magictool bib.bib tversky83
> Tversky, Amos, and Daniel Kahneman. "Extensional versus intuitive reasoning: The conjunction fallacy in probability judgment." Psychological review 90 (1983): 293--315.


Does such a tool exist? If not, could you give me some pointers on how to build one? Presumably it would involve parsing the bib file in much the same way that bibtex does.

-
I guess you could generate a tex file that consisted only of the citation (with the appropriate formatting commands in preamble) and then compile it, and then use pdftotext to create a plain text version. But that seems a long way round. –  Seamus Nov 8 '12 at 13:01
Gimme ten minutes, I'll try to write one. :) –  Paulo Cereda Nov 8 '12 at 13:04
@PauloCereda That's the spirit! –  Seamus Nov 8 '12 at 13:04
Just remembered that Pandoc can manage references, so it might be worth looking at how that does it. Although pandoc is written in Haskell, so that might be more trouble than it's worth! –  Seamus Nov 8 '12 at 13:13
There are Perl libraries that can parse BibTeX syntax (see, for example, search.cpan.org/~ambs/Text-BibTeX-0.66/lib/Text/BibTeX.pm) –  Andrew Stacey Nov 8 '12 at 13:25

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:

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:

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:

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 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. :)
-
I wish my lunch-times were this productive! –  Seamus Nov 8 '12 at 15:27
Clearly you had the wrong date services.w3.org/… –  David Carlisle Nov 8 '12 at 15:40
@DavidCarlisle: ♥ –  Paulo Cereda Nov 8 '12 at 15:43
Another tool! Paulo's productivity ---> \infty :-) –  Harish Kumar Nov 8 '12 at 15:47