Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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>
Thank you all in advance.#/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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: split file
by GrandFather (Saint) on Feb 09, 2006 at 04:36 UTC | |
by Anonymous Monk on Feb 10, 2006 at 04:51 UTC | |
by GrandFather (Saint) on Feb 10, 2006 at 05:46 UTC | |
by Anonymous Monk on Feb 13, 2006 at 21:33 UTC | |
by GrandFather (Saint) on Feb 14, 2006 at 11:06 UTC | |
|