Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floriandierickx/9c93f73c0cc7bca5b56c9a77f39e1916 to your computer and use it in GitHub Desktop.
Save floriandierickx/9c93f73c0cc7bca5b56c9a77f39e1916 to your computer and use it in GitHub Desktop.
Takes a set of exported bibtex collections from Zotero using Better BibTeX plugin, converts them to bibliography entries in markdown format with sub-headings for each collection.
#!/bin/sh
## Introduction
# Can be used to create a merged bibliography from a set of exported collections
# This is a slightly modified version of https://gist.github.com/kmlawson/3bdfd917ef55969faec7768c140dcdaa
# that works inside a conda-environment on windows
# Run this code using 'bash zotero-collection-merge.sh' inside folder with bibtexfiles, after having
# exported bib-files with BetterBibTex according to https://forums.zotero.org/discussion/88163/export-bibliography-but-preserve-hierarchy-of-sub-folders
# To convert md-file to docx afterwards: pandoc -o output.docx -f markdown -t docx bibliography.md
## Conda environment
# Required conda packages: pandoc, m2-base (includes sed)
# Conda environment activation (here: base) with pandoc & m2-base:
eval "$(conda shell.bash hook)"
conda activate base
# list files by name length:
MAINCOLLECTION=`ls *.bib | perl -e 'print sort { length($a) <=> length($b) } <>' | head -n 1`
MAINROOT=$(echo "$MAINCOLLECTION" | cut -f 1 -d '.')
rm -f "$MAINCOLLECTION Bibliography.md" # In case we already created one before
for FILE in *.bib
do
echo "Converting \"$FILE\" to markdown."
FILEROOT=$(echo "$FILE" | cut -f 1 -d '.')
# You need pandoc installed
pandoc "$FILE" -s --citeproc -t markdown_strict -o "$FILEROOT.md"
REMOVEMAIN=`echo $FILEROOT | sed -e "s/$MAINROOT-//g"`
# This version uses sed from m2-base on Windows (anaconda environment)
sed -E -i "1i &nbsp;" "$FILEROOT.md" # force blank line
sed -E -i "1i ## $REMOVEMAIN" "$FILEROOT.md" # add collection name at top as level 2 header
sed -E -i "1i &nbsp;" "$FILEROOT.md" # force blank line
done
OTHERS=`ls *.md | grep -v "^$MAINROOT.md$"`
echo "Merging files with sub-headings"
IFS=$'\n'
mv "$MAINROOT.md" "$MAINROOT Bibliography.md"
for FILE in $OTHERS # using loop instead of xargs due to possible long list
do
echo "Merging $FILE"
cat "$FILE" >> "$MAINROOT Bibliography.md"
# Uncomment if you want to clean up and delete all the md files except the merged one:
# rm "$FILE"
done
# Uncomment if you want to clean up and delete all the exported bib files:
# rm *.bib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment