How to sort (by whatever key) BibTex entries in `.bib` file

gitlatexsorttext processing

I work with LaTeX and do versioning with Git. For bibliography management I use Mendeley.

The problem is that each time Mendeley synchronizes it's .bib exports,
they are in different order, what makes bibliography versioning much harder.

My idea is to sort BibTex entries in .bib file, each time before commit.

Could you help me, how to do this in smart (short&sweet) way ? 🙂

P.S. I can run this routine manually. I do not need git integration. I just want program/script to sort .bib file.

Best Answer

How about bibsort?

NAME
     bibsort - sort a BibTeX bibliography file

SYNOPSIS
     bibsort [optional sort(1) switches] < infile >outfile

DESCRIPTION
     bibsort filters a BibTeX bibliography, or bibliography frag-
     ment,  on  its standard input, printing on standard output a
     sorted bibliography.

It's a shell script wrapping nawk (and tr, sort and grep) and includes two warnings you might have to pay attention to (see the source).

(Edit There're also a lot of bibtex-related Perl modules...)

Edit2 I just recognized you'd like to sort for any key, while bibsort apparently sorts by the bibtex tags only -- but maybe its source (it's not too long) is still helpful...?

Related Question