This can be performed by the bib2bib tool, which provides pretty flexible and reliable ways to filter/extract/expand bibtex entries. This (little known) utility is part of the bibtex2html tool suite. (Note: you have to look for the PDF documentation, the HTML documentation does not discuss bib2bib!)
For instance, to flatten a bib file, one just writes:
bib2bib --expand --expand-xrefs -ob flattened.bib original.bib
It is also possible to specify filter and sorting options.
Multiple conditions can be grouped and combined with and/or/not to extract only entries matching certain criteria.
Edit: One thing that apparently is not supported is filtering out @comment entries. These entries are, for instance, inserted by JabRef. To get rid of them just pipe the result through some regex tool. Jukka suggested perl -p0 -e 's/\s*(\@comment\{\{[^}]*\}\}\s*)+/\n\n/g' for the job.
bib2bibseems to preserve all@commententries, no matter what I do. Even if I use the filtering options, I seem to always get all@commententries... – Jukka Suomela Oct 12 '11 at 21:30@commententries. If your@commententries are single-line only (like those added bybib2bibitself), you may just filter the resulting file through sed/grep/awk to get rid of them, e.g.grep -v "@comment" flattened.bib > nocomment.bib. – Daniel Oct 12 '11 at 21:38--no-commentto preventbib2bibfrom adding any more comments, but it still preserves all existing comments. In my case I have lots of multi-line comments that are created by JabRef, and they are a bit more tricky to filter out... – Jukka Suomela Oct 12 '11 at 21:44perl -p0 -e 's/\s*(\@comment\{\{[^}]*\}\}\s*)+/\n\n/g'happens to work in my specific case.) – Jukka Suomela Oct 12 '11 at 22:00