I am currently working on a project where I need to read in a file that contains variable length data and then to restructure it into a fixed width file type. I can open and read the entire file and put it through STDOUT, but what I am having trouble doing is I need to be able to say start record at this constant value then read down a total of 17 lines and assign each of those to a different value, then I need to read a variable number of line length section that eventually ends at a constant and put those into individual lines in a different file. What I essentially need is a way to say start at this character read each line for the next 17 and assign to values to do a printf statement for the first file which would be the header file, then I need to be able to say starting at the 18th line of that record read until the end constant and print that straight to a different file which is the detail section of the record, and then start the entire loop again. The file I am needing to break down can contain hundreds of records that need to be split out this way. While I can read the initial file in and then push it out line for line, I have not been able to do the assignments at all. Any suggestions would be immensely helpfull! Thank you!

Update - Thanks for the responses fella's, I can provide a generic set of data, obviously since what i am working on is bank data I can't supply an actual data file. File appears as such

^L ( identifies beginning of record in file) John Sample 123 MY ROAD Somewhere, Country ZIP + account# + account# begin-period-date end-period-va +lue message to include 1 message to include 2 (line 17 should begin here as detail file with varying length. can be +1 line or 100 lines) (EOE) - which is end of record marker

what I need to do is take each record starting at ^L and assign those lines to specific values so that I can then place it into a fixed width header file with this format:

<account#><name><address><city state zip><begin-period-date><end-perio +d-date>

then the detail file, is really just a matter of reading line by line until the end and just dumping it into a second file.

what I have so far is really just garbage to be honest. I have this for opening the file

open(ADDRESSIN, "filename") || die "could not open file for read: $!\n +"; while($line = <ADDRESSIN>) "this is where I need help" $line1 = <name> $line2 = <address>

I am unsure as to how to tell it to start at ^L, go down 3 lines and assign that to $line1, then to next line and assign to $line2, etc.

#to print out I am using this, or was planning on using that I should say

printf "%-40s%-40s\n", $line1, $line2,$line3;

does this help make it more clear with what I am trying to do? well crap, I just updated and the formatting is off so it's just garbled, anyone tell me how to format this so it's legible?

Edit: g0n - formatting tags

Gents, I thank you greatly for your assistance. With what you all gave me I was able to work out a snippet that works nicely. Again. I thank you all!

In reply to File read and re-ordering by KarmicGrief

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.