1756020 Members
2587 Online
108839 Solutions
New Discussion

perl script

 
Sasirekha
Occasional Contributor

perl script

Hi...

I wanted split a file and move certain no of lines from that file to another.

i.e I need to take out first 800 lines in a file and move to a new file and then the next 800 to another new file,etc...

I wrote the below coding,but I could not take out the line from the old file...plz help me out in finding the mistake in my code

#!/usr/bin/perl

use strict;
use warnings;
my $logfiles_dir_path = '/home/rsaravanan/rekha/assignments/log';
opendir DH, $logfiles_dir_path or die "Cannot open dir $logfiles_dir_path : $!";
foreach my $logfilename (readdir DH)
{
my $logfile = $logfiles_dir_path .'/'.$logfilename;
open(FH, $logfile) or die "Can't open $logfile: $!";;
my @file_lines = ;
close(FH);
my $line_count = 0;
while (@file_lines)
{
my $i = 1;
#my $line_count = 0;
my $new_filepath = $logfiles_dir_path .'/'.$logfilename.'.'.'file'.$i;
open DATAOUT, ">>$new_filepath" or die "can't open $new_filepath $!";
#until($line_count == 5)
#{
foreach my $line (@file_lines)
{
my $out = shift @file_lines;
#print $out;
print DATAOUT "$out";

}
# $line_count++;

# }
close(DATAOUT);
$i++;

}

}
close(DH);

1 REPLY 1
H.Merijn Brand (procura
Honored Contributor

Re: perl script

However much I love perl, why not use split or csplit here?
The right tool for the right problem.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn