#!/usr/bin/perl my $file = shift; bad_format() if ($file eq "" ); open FILE, $file or die "Could not open file [$file]\n"; print "file : $file\n"; #Uncomment this section and things go goofy #$file =~ m/(\w+)\..*/; #my $fname = $1; ### my %files = (); # Hash with file prefix and handle my $line; while ($line = ) { $line =~ m/^(.{6}).*/; my $ffc = $1; if ($ffc ne "" ) { my $check = 0; foreach my $key(%files) { $check = 1 if ($ffc eq $key); } if ($check == 0) { print "Adding new handle : $ffc\n"; local *FH; open (FH, ">$ffc.txt") or die; #open (FH, ">$fname_$ffc.txt") or die; # I want to save the file as this format $files{$ffc} = *FH; } my $f = $files{$ffc}; print $f $line; #print "writing to $key\n"; } } while (my ($key, $value) = each (%files)) { print "Closing $key\n"; close $value; } close FILE; sub bad_format { print "\nformat: split \n\n"; exit; } #### #!/usr/bin/perl # Splits a data file into unique files based on each lines first 6 characters use warnings; use strict; my $file = shift; bad_format() if ($file eq "" ); open FILE, $file or die "Could not open file [$file]\n"; my ($fname) = $file =~ m/(\w+)\..*/; my %files = (); while (my $line = ) { if ($line !~ /^\s*$/) { my $fc = substr($line, 0, 6); # first characters if (!exists $files{$fc}) { open ($files{$fc}, ">$fname\_$fc.txt") or die; } print {$files{$fc}} $line; } } while (my ($key, $value) = each (%files)) { print "Created $fname\_$key.txt\n"; close $value; } close FILE;