There are many ways to accomplish this task. If it's a one-time deal, you can do something quick-n-dirty:

#!/usr/bin/perl -nl chomp; @record = split /\|/, $_; print "INSERT INTO table (foo, bar, baz)" , " VALUES ('$record[0]', '$record[1]', '$record[2]')";

Then run it with something like:

    ./yourscript < inputdata > output.sql

And you'd end up with a .sql file you could run against the database.

Now, if you need something more reliable and reusable, you'll probably want to wrap your own I/O code rather than letting perl -n take care of it for you (for more on this, you can see PerlMonks' Basic Input and Output tutorial). You'd probably want to insert the data directly into the database with DBI (make sure you use placeholders, as documented in the DBI docs). The basic idea about splitting the lines, as my quick-n-dirty snippet demonstrates, would be the same.

Update: Oh yeah, also forgot to mention. For anything other than a one-off throwaway script, you should probably use strict and warnings, unless there's a good reason not to.


In reply to Re: Parsing Pipe Delimited Multi-line File by revdiablo
in thread Parsing Pipe Delimited Multi-line File by LinuxBoxRocks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.