Skip to content

Instantly share code, notes, and snippets.

@kmlawson
Created March 16, 2021 17:46
Show Gist options
  • Save kmlawson/3bdfd917ef55969faec7768c140dcdaa to your computer and use it in GitHub Desktop.
Save kmlawson/3bdfd917ef55969faec7768c140dcdaa 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
# Can be used to create a merged bibliography from a set of exported collectiions
# 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 | gsed -e "s/$MAINROOT-//g"`
# I use gsed with coreutils on Mac OS X
gsed -E -i "1i &nbsp;" "$FILEROOT.md" # force blank line
gsed -E -i "1i ## $REMOVEMAIN" "$FILEROOT.md" # add collection name at top as level 2 header
gsed -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