Skip to content

Instantly share code, notes, and snippets.

@novoid
Created December 26, 2015 10:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save novoid/7c3bf7360e8471364560 to your computer and use it in GitHub Desktop.
Re-generating reference file for Bibtex
#!/bin/bash
## this script parses *.aux files (LaTeX/bibtex temporary files) in
## the current directory, extracts cited references and re-generates
## a bibtex-file
RESULTFILE=newreferences.bib
handle_outputfile_found()
{
echo
echo "ERROR: I already found file \"${RESULTFILE}\". To prevent data loss, I quit now."
echo
exit 1
}
handle_no_auxfile_found()
{
echo
echo "ERROR: I found no file with an \".aux\" extension in the current folder."
echo
exit 2
}
handle_no_bibtexfile_found()
{
echo "WARNING: I found no bibtex-file \"${1}\". Continuing ...."
}
[ -f "${RESULTFILE}" ] && handle_outputfile_found
touch "${RESULTFILE}"
#echo "Creating new output reference file: ${RESULTFILE}"
numaux=`ls -l *aux | wc -l`
[ ${numaux} -eq 0 ] && handle_no_auxfile_found
referredpapers=`grep citation *.aux | sed 's/\\\\citation{//' | sed 's/}//'| sort | uniq`
for currentref in ${referredpapers}; do
#echo "do: ${currentref}";
nextfile="${HOME}/archive/library/${currentref}.bib"
if [ -f "${nextfile}" ]; then
cat "${nextfile}" >> "${RESULTFILE}"
else
handle_no_bibtexfile_found "${nextfile}"
fi
done
echo "successfully finished writing: ${RESULTFILE}"
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment