Hi all, I have a main file that contains multiple bills and I want to split the file into individual bills. I have a script that splits the file when the header line is identified for a new bill and writes this data to an individual file. But the problem is the bill detail is not always located right after the header info. For eg. the file may contain header info for 2 bills in a row and then the bill detail (or any other order). Thus I want to split the file at both the header line and bill detail line and merge them separately according to the bill number. I'm not sure how to add this logic to the existing script.

DATA:
1010100.........123456..... #header line for bill # 123456 #some data #some data #some data #some data 1010100.........678910..... #header line for bill # 678910 #some data #some data #some data 3010330.........123456..... #bill detail for bill 123456 #some data #some data #some data #some data #some data 3010330.........678910..... #bill detail for bill 678910 For eg. each bill should be split and joined like this: 1010100.........123456..... #header line for bill # 123456 #some data #some data #some data #some data 3010330.........123456..... #bill detail for bill 123456 #some data #some data #some data #some data #some data <br>



This's what I have right now.
#/usr/bin/perl -w #This script takes a main file and separates into individual bills by +splitting at the header line. # [-v|--verbose] # [-d|--dest splitdir] filename # use Getopt::Long; GetOptions("verbose" => \$verbose, "dest:s" => \$destdir); if (length $destdir == 0) { $split_file_prefix = $ARGV[0]; } else { $split_file_prefix = $destdir."/".`basename $ARGV[0]`; chomp($split_file_prefix); } ## open file name passed in as arg 1 for reading open(MAIN_BATCH_FILE, "<$ARGV[0]") or die "Couldn't open file : $ARGV[0] \n Message : $!"; $first_line = <MAIN_BATCH_FILE>; $counter = 1; $filename = $split_file_prefix."-SPLIT-".$counter.".txt"; if ($verbose) { print "$filename\n"; } open(NEW_FILE,">".$filename); print NEW_FILE $first_line; while (<MAIN_BATCH_FILE>) { if ($_ =~ /^1010100/) { close(NEW_FILE); $counter++; $filename = $split_file_prefix."-SPLIT-".$counter.".txt"; if ($verbose) { print "$filename\n"; } open(NEW_FILE,">".$filename); } print NEW_FILE "$_"; } close(NEW_FILE); exit 0;
Thank you all in advance.

In reply to split file by Anonymous Monk

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.