#!/bin/bash # extract bib items from a source bibtex database for a given tex file # usage: # extractBib tex-file bibtex-database output-bibtex-file # e.g., # extractBib mypaper.tex mybibtexdatabase.bib mypaper.bib # http://scriptdemo.blogspot.com function getkeys() { texfile=$1 mykeys=`grep "cite.*{.*}" ${texfile} | sed -e 's/\ //g' | awk '{for (i=1; i<=NF;i++) {if ($i ~ /cite/) {print $i}}}' | sed -e 's/\}/\n/g' | awk -F\{ '{print $2}' | sed -e 's/\,/\n/g' | sed '/^[\ ]*[\ ]*$/d' | sort | uniq | sed '/^\\\\/d' | sed '/:/d'` echo ${mykeys} } if [ $# -eq 3 ]; then texfile=$1; bibfile=$2; outfile=$3 elif [ $# -eq 2 ]; then texfile=$1; bibfile=$2; outfile="extractbib_"$1 else echo "usage:" echo " extractBib tex-file bibtex-database output-bibtex-file" exit fi # make sure no overwrite [ -e ${outfile} ] && echo "${outfile} exists alreay! exit" && exit allkeys=`getkeys ${texfile}` if [ ${#allkeys[*]} -ne 0 ]; then touch ${outfile} for eachkey in ${allkeys[*]} do #echo ${eachkey} sed -ne "/${eachkey}/,/^[\ ]*\}$/p" ${bibfile} >> ${outfile} done fi |
Sunday, April 7, 2013
[Bash] extractBib: extract bib items from a source bibtex database for a given tex file
Subscribe to:
Posts (Atom)