awk code to convert some BiBTeX to SYLK (was Re: BibTex to SYLK?) 
Author Message
 awk code to convert some BiBTeX to SYLK (was Re: BibTex to SYLK?)

  [ this message contains pointless tabs; ignore them if you wish ]

Quote:

> Does anyone have script or code to convert a .bib file into something
> easily read by a database program, such as SYLK?

I don't use BiBTeX, so I just whipped something together, to try on some
files I found on news.  I hope it works as well for you.

I have no doubt that there are many .bib files for which this will not
quite work.  but I also know it _does_ work on ones I've tried.

line 1 keeps track of the field names, which should be helpful for
importing into a database.  it dynamically assigns fields as it finds
them in your data; if you want to force an order, just add more
`addtodb()' calls in the BEGIN block.

===
# bib2slk -- convert some BiBTeX to SYLK files

# 1998/Nov/10

# assumes there are no tabs in the file

BEGIN \
{
# SYLK header
        print "ID;PBIB2SLK";

        q="\"";

# row 1 is used for the headers; this will be incremented before use
        rownum=1;

        initdb();
        addtodb("header");

        linesofar="";

Quote:
}

function initdb()
{
        numdb=0;

Quote:
}

function addtodb(afieldname)
{
        numdb++;
        positions[afieldname]=numdb;
        print "C;Y1;X" numdb ";K" q afieldname q;

Quote:
}

# strip leading and trailing whitespace
/^ / { sub(/^ */,""); sub(/ *$/,""); }



{
        oldpart=$0;
        newpart=$0;

# remove everything up to and including first {, and first = onwards
        sub(/^[^{]*{/,"",oldpart);
        sub(/ *=[^=]*$/,"",oldpart);

# remove everything up to and including the =, and the closing }
        sub(/^[^=]*= */,"",newpart);
        sub(/}$/,"",newpart);

        replacements[oldpart]=newpart;

        next;

Quote:
}

# make header line look like other lines

{
        sub(/.*{/,"");
        sub(/,$/,"");
        $0="header = " q $0 q ",";
        rownum++;

Quote:
}

# strip off } from last line
/}$/ { sub(/}$/,","); }

# ignore blank lines
/^ *$/ { next; }

# combine multi-line entries
{
        if (linesofar=="")
                linesofar=$0;
        else
                linesofar=linesofar " " $0;

Quote:
}

# handle terminated lines
/,$/ \
{

# separate field name from rest of data
        fieldname=linesofar;
        sub(/ *=[^=]*$/,"",fieldname);

# remove field name, =, and trailing comma
        sub(/^[^=]*= */,"",linesofar);
        sub(/,$/,"",linesofar);


        if (linesofar in replacements)
                linesofar=replacements[linesofar];

# remove wrapping quotes
        if (linesofar ~ /^".*"$/)
        {
                sub(/^"/,"",linesofar);
                sub(/"$/,"",linesofar);
        }

        if (! (fieldname in positions))
                addtodb(fieldname);

# SYLK requires doubling of " and ; in strings (I thought), but with
# MS Excel, only the ; needs to be doubled
#       gsub(/"/,q q,linesofar);
        gsub(/;/,";;",linesofar);

        print "C;Y" rownum ";X" positions[fieldname] ";K" q linesofar q;

        linesofar="";

Quote:
}

END { print "E"; }
===

here's what I tried it on:



        author = "S. K. Chang and A. Gill",
        title = "Algorithmic solution of the change-making problem",
        journal = JACM,
        volume = 17,
        year = 1970,
        pages = "113-122"}

        author = "J. W. Wright",
        title = "The change-making problem",
        journal = JACM,
        volume = 22,
        year = 1975,
        pages = "125-128"}

        author = "M. J. Magazine and G. L. Nemhauser and Trotter, Jr., L. E.",
        title = "When the greedy solution solves a class of knapsack problems",
        journal = OR,
        volume = 23,
        year = 1975,
        pages = "207-217"}

        author = "T. C. Hu and M. L. Lenard",
        title = "Optimality of a heuristic solution for a class of knapsack
problems",
        journal = OR,
        volume = 24,
        year = 1976,
        pages = "193-196"}

        author = "L. Chang and J. F. Korsh",
        title = "Canonical coin changing and greedy solutions",
        journal = JACM,
        volume = 23,
        year = 1976,
        pages = "418-422"}

        author = "D. Kozen and S. Zaks",
        title = "Optimal bounds for the change-making problem",
        journal = TCS,
        volume = 123,
        year = 1994,
        pages = "377-388"}

        author = "J. D. Jones",
        title = "Orderly currencies",
        journal = AMM,
        volume = 101,
        year = 1994,
        pages = "36-38",
        note = "Main theorem at the bottom of page 37 is incorrect; a
counterexample is (1,10,25)"}

        author = "S. Maurer",
        title = "Comment:  orderly currencies",
        journal = AMM,
        volume = 101,
        year = 1994,
        pages = "419"}
===
--



Sat, 28 Apr 2001 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. awk code to parse SYLK files

2. Specifications for SYLK format?

3. Specifications for SYLK format?

4. reading and writing Spreadsheet Tables in SYLK format

5. APL Bibliography in BiBTeX

6. Bibtex entry for pickaxe book?

7. FTP-able bibliography on partial evaluation (Bibtex)

8. Scheme Bibliography - bibtex format (generated)

9. Scheme BibTex file

10. references of scheme textbooks in bibtex

11. CHDL 91 Proceedings - BibTeX file

12. BibTeX module?

 

 
Powered by phpBB® Forum Software