Skip to content

Instantly share code, notes, and snippets.

@dmentipl
Created July 11, 2019 00:34
Show Gist options
  • Save dmentipl/8f53ae0cd89cdfc08629297697799fda to your computer and use it in GitHub Desktop.
Save dmentipl/8f53ae0cd89cdfc08629297697799fda to your computer and use it in GitHub Desktop.
Make a shortened .bib file from citations in a tex file
#!/bin/bash
#
# Takes a .tex file and finds citations of the form:
#
# \cite{mentiplay:2018}
# \citet{mentiplay:2018}
# \citep{mentiplay:2018}
# \cite{mentiplay:2018a}
# \citet{mentiplay:2018b}
# \citep{mentiplay:2018c}
#
# and then uses those cite keys to strip the particular references from a .bib
# file and puts them into a shorter .bib file with only the references from the
# .tex file.
#
# It assumes that the .bib file has each reference as a separate paragraph.
#
# D. Mentiplay, 2018.
if [ "$#" -ne 2 ]; then
echo "Usage ./make_short_bib.sh texfile.tex bibfile.bib"
return
fi
TEXFILE=$1
BIBFILE=$2
REFS=($(grep -ohE '\\cite.*?:\d\d\d\d[a-z]*}' "${TEXFILE}" |
cut -d'{' -f 2 |
cut -d'}' -f 1 |
awk '{gsub(/, /,"\n")}1' |
awk '{gsub(/,/,"\n")}1' |
sort |
awk '!seen[$0]++'))
for REF in "${REFS[@]}";
do
awk -v RS='' "/${REF},/" "${BIBFILE}"
echo ''
done > references-short.bib
echo "See references-short.bib"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment