# A program to clean up end note output # # awk -v prefix=ABC -v outfile=filename -f endnotetobib.awk infilename.text # where ABC is a 2-3 letter prefix for the keys in the output file # filename is the name of the output filename ###################################################################### BEGIN{ i=0; numconverted = 0; if ( length(prefix) < 1 ) { prefix = "RSM" } if ( length(outfile) < 1 ) { outfile = "output.bib"; } lastkey=""; repsuffix[1] = "a"; repsuffix[2] = "b"; repsuffix[3] = "c"; repsuffix[4] = "d"; repsuffix[5] = "e"; } /@/ { line1 = $0; line = $0; numlines = 0; qfoundauth = 0; while ( length($0) > 1 ) { numlines = numlines + 1; thelines[numlines] = line; bigline = bigline " " $0; getline; line = $0; } # printf(" The full entry is %s\n", bigline); # printf("the first lines are: %s\n%s\n%s\n%s\n", # thelines[1], thelines[2], thelines[3], thelines[4]); for (linenum=1; linenum <= numlines; linenum++) { # MAke the new key line if ( linenum == 1 ) { cnum = index(thelines[1], "{"); bibtype = substr(thelines[1], 1, cnum); # printf("cnum = %d and bibtype is %s\n", cnum, bibtype); # Now find the authors } else if ( index(thelines[linenum], "Author") > 0 ) { nameline = substr(thelines[linenum], 1); split(nameline,thenames," "); authname = thenames[3]; gsub(/-/, "", authname); gsub(/{/, "", authname); gsub(/}/, "", authname); gsub(/'/, "", authname); gsub(/\"/, "", authname); gsub(/\\/, "", authname); firstauth = substr(authname, 1, 3); if ( index(firstauth,",") > 0 ) { cnum = index(firstauth,","); firstauth = substr(firstauth,1,cnum-1); } qfoundauth = 1; if (linenum == 2) { qodd = 1 } else { qodd = 0 } # printf("first author is %s from %s\n", # firstauth, thelines[linenum]); # Find the year } else if ( index(thelines[linenum], "Year") > 0 ) { cnum = index(thelines[linenum], "{"); theyear = substr(thelines[linenum], cnum+1, 4); if ( theyear < 2000 ) { yearstring = substr(theyear,3,2); } else { yearstring = theyear; } # printf(" yearstring is %s from %s\n", # yearstring, thelines[linenum]) } } # The key for the bibtex entry key = prefix ":" firstauth yearstring; printf(" Key: %s\n", key); if ( key == lastkey ) { repcount = repcount + 1; key = key repsuffix[repcount]; printf(" Found double for key = %s so set to %s\n", lastkey, key); } else { repcount = 0; lastkey = key; } # Now create the Bibtex entry printf("%s%s,\n", bibtype, key ) > outfile; if ( qfoundauth == 0 ) { printf("No authors in this entry!\n"); } if ( qodd == 1 || qfoundauth == 0 ) { firstnum = 2; } else { firstnum = 3; } for (linenum=firstnum; linenum <= numlines; linenum++) { printf("%s\n", Packentry(thelines[linenum])) > outfile; } printf("\n\n") > outfile; numconverted = numconverted + 1; } END{ printf("Converted %d entries\n", numconverted); } ###################################################################### function Packentry( entryin ) { # Clean up and pack an entry, get rid of funky characters or make the # proper LaTeX special characters gsub(/\|/, " ", entryin ); gsub(/ /, " ", entryin ); gsub(/\"/, "", entryin ); gsub(/%/, "\\%", entryin); gsub(/\$/, "\\\$", entryin); gsub(/\#/, "\\\#", entryin); return entryin; }