For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2004 > splitting files (was Re: Joining Lines.)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author splitting files (was Re: Joining Lines.)
Wc -Sx- Jones

2004-04-23, 3:32 pm

Jeff 'japhy' Pinyan wrote:
> my @record = map scalar(<IN> ), 1 .. 6;



It's a neat trick :)

Same trick, but to split up *any text* files -

#! /usr/local/bin/perl

use strict;
use warnings;

# Example command:
# perl split_files syslog [3]
# where [] is an optional number of files to split into...

# This version is somewhat slower than the earlier version.

my $filename = shift || die "no file name given...";
my $split_into = shift || 3;
my $line_cnt;
my $x;

open (ROFILE, "$filename") or die "cannot open $filename $!";
while(<ROFILE> ) { ++$line_cnt; }
close (ROFILE) or die "cannot close $filename $!";

open (ROFILE, "$filename") or die "cannot re-read $filename $!";

for ($x=0; $x < $split_into; ++$x) {

open (WOFILE, ">$filename.$x") or die "cannot write to $filename.$x $!";
until (eof ROFILE) {
my @data_lines = map scalar(<ROFILE> ), 1 ..
($line_cnt/$split_into);
print WOFILE @data_lines;
last;
}

close (WOFILE) or die "cannot close $filename.$x $!";
}

close (ROFILE) or die "cannot close $filename $!";
print "Done ... \n\n";

__END__

--
_Sx_ http://youve-reached-the.endoftheinternet.org/
________________________________________
_________________________
perl -MMIME::Base64 -e 'print
decode_base64(" SnVzdCBvbmUgbW9yZSBQb3N0Zml4IDIuMnggU25h
cHNob3QgRmlsdGVyLCBwbGVhc2UK");'
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com