Skip to content

Instantly share code, notes, and snippets.

@kmader
Created December 18, 2014 21:02
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 kmader/ac6af570d28779d409e7 to your computer and use it in GitHub Desktop.
Save kmader/ac6af570d28779d409e7 to your computer and use it in GitHub Desktop.
Delete duplicate entries from a bibtex library (only the first)
bibstr='\n'.join(open('library.bib').readlines())
bibents=bibstr.split('@article')
def get_key(tstr):
(stpos,endpos) = (tstr.find('{'),tstr.find(','))
if(stpos>=0) & (endpos>stpos): return tstr[stpos+1:endpos]
else: return tstr # keep everything
biblist=map(lambda x: (get_key(x),x),bibents)
outlist=[]
keyalready={}
for (cKey,cVal) in biblist:
if keyalready.get(cKey,0)<1:
outlist+=[cVal]
keyalready[cKey]=1
with open('library_dupfree.bib', 'w') as f:
f.writelines("%s\n" % l for l in ('@article'.join(outlist)).split('\n'))
@nkoester
Copy link

thanks so much for this <3

@mar-esther23
Copy link

Thanks, this was extremely useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment