Created
July 5, 2012 21:48
-
-
Save jfrazee/3056704 to your computer and use it in GitHub Desktop.
Cut unused entries from .bib files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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