I am a Perl neophyte when it comes to reading and parsing files. I am definitely in need of wisdom.

I have a 3000 line text file that is comprised of 46 character lines:

C4432882490H019000020150211ESL6690 0H2015PC
C4833076550HC0P0000201412093J46651 0H2015DX
C6033106980H057130020150323FRE7602 0H2015PC
C663160140MT007015G20141124274847A MT2015PC

Character 1 (or 0?) is a field, Characters 2-16 are a field, 17-30 another, and finally 31-46 are the last field. I want to read the file from /tmp (filename is always the same and always in the same place) and parse it into the fields (the counts never change) and then save it as a comma-delimited version.

Since the fields never change length it would seem that unpack is a better choice than substr, is that correct?

Is there a simple bit of code that someone might get me started with, please? Thank you!


In reply to Unpack or substr to create CSV? by johnmck

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.