Skip to content

Instantly share code, notes, and snippets.

@Fitzy1293
Last active August 29, 2021 06:22
Show Gist options
  • Save Fitzy1293/5c6019479456054b267764354df2a899 to your computer and use it in GitHub Desktop.
Save Fitzy1293/5c6019479456054b267764354df2a899 to your computer and use it in GitHub Desktop.
Shell script to get BibTeX citations from PDF. For use in .bib files that you use to do references in LaTeX.
#!/bin/env bash
#https://gist.github.com/Fitzy1293/5c6019479456054b267764354df2a899
if [ $# -eq 0 ]; then
echo "Enter a pdf to create a bibtex citation for"
ls *.pdf | tr ' ' '\n'
exit 1
fi
if [ ! -f $1 ]; then
echo "$1 does not exist"
exit 1
fi
inFnameNoExtension="$(echo $1 | cut -d "." -f 1)"
outFname="$inFnameNoExtension.txt"
pdftotext -layout $1 $outFname -l 25
# ==============================================================================================
# Tries to get a bibtex citation from a doi.
doiFind="$(awk '/doi:/+/DOI:/' $outFname | head -n 1 | sed -e 's/\s//g' -e 's/.*://' | grep -o -E "[0-9]|\.|\/")"
doi="doi:$(echo $doiFind | sed 's/ //g')"
crossrefEndpoint="http://api.crossref.org/works/$doi/transform/application/x-bibtex"
crossref="$(curl -s $crossrefEndpoint)"
if ! echo $crossref | head -n 1 | grep -q "Resource" ; then
echo "$crossref"
exit 0
fi
# ==============================================================================================
# Tries to use an isbn to get bibtex.
# Example isbn for boas, math methods in the physical sciences
#ISBN 0-471-19826-9
#0471198269
isbn="$(grep -m 1 -o "ISBN .*" $outFname | sed 's/[^0-9]*//g')"
isbnSearch="$(curl -s "https://www.ottobib.com/isbn/$isbn" | grep -A 10 "@Book")"
bookBibReference="$(echo $isbnSearch | grep -o "@Book.*"\
| sed -e 's/<.*//'\
-e 's/}, /},\n/g'\
-e 's/, author/,\nauthor/'\
-e 's/} }/}\n}/')"
echo "$bookBibReference" | head -n 1
echo "$bookBibReference" | grep -i "^[a-z]" | sed 's/^/\t/g'
echo "$bookBibReference" | tail -n 1
# ==============================================================================================
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment