Skip to content

Instantly share code, notes, and snippets.

@jfrazee
Created July 5, 2012 21:48
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 jfrazee/3056704 to your computer and use it in GitHub Desktop.
Save jfrazee/3056704 to your computer and use it in GitHub Desktop.
Cut unused entries from .bib files
require 'set'
require 'bibtex'
# Usage: ruby citecut.rb <latex> <bib>
# Get the citekeys used in the LaTeX doc
citekeys = Set.new
open(ARGV[0]).readlines.each do |line|
line.scan(/\\\w*cite\w*{([\w\d,:-]+)}/) do |c|
citekeys.merge c[0].split(',')
end
end
# Create a new .bib file w/ only the used entries
bib = BibTeX.open(ARGV[1])
citekeys.sort.each do |c|
if bib[c].nil?
bib.each do |entry|
if entry.key.downcase == c.downcase
entry.key = c
warn "updating citekey: #{c}"
break
end
end
end
puts "#{bib[c]}\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment