http://www.perlmonks.org?node_id=1023909

hyans.milis has asked for the wisdom of the Perl Monks concerning the following question:

hi

actually i would like to move the line into different file if prefix matching 628. could u please advice about the code as the output is not correct. thanks

File Input
621001000286 2/13/2013 18:42 3/15/2013 18:42 627001000298 3/13/2013 13:20 4/12/2013 13:20 628001000796 3/12/2013 19:07 4/11/2013 19:07 628001000823 3/13/2013 13:28 4/12/2013 13:28 629001001592 2/8/2013 15:52 3/10/2013 15:52
Output1
621001000286 2/13/2013 18:42 3/15/2013 18:42 627001000298 3/13/2013 13:20 4/12/2013 13:20 629001001592 2/8/2013 15:52 3/10/2013 15:52
Output 2
628001000796 3/12/2013 19:07 4/11/2013 19:07 628001000823 3/13/2013 13:28 4/12/2013 13:28
#!/usr/bin/perl use strict; use warnings; my $file = $ARGV[0] or die "Need to get CSV file on the command line\n +"; my $outfile = $ARGV[1] or die "Need to get output file on the command +line\n"; my $sum = 0; open(my $data, '<', $file) or die "Could not open '$file' $!\n"; open (OUTFILE, "> $outfile") || die "ERROR: opening $outfile\n"; open (OUTFILE_1, "> 1$outfile") || die "ERROR: opening $outfile\n"; while (my $line = <$data>) { chomp $line; my @fields = split "," , $line, -1;; my $sum = "$fields[0]"; #$fields[1] = "$sum"; if ($sum =~ /^628?$/ ) { #s/^628+//; $line= join( ",", @fields ) . "\n" ; print OUTFILE ("$line"); } else { $line= join( ",", @fields ) . "\n" ; print OUTFILE_1 ("$line"); } } close ($data); close (OUTFILE); close (OUTFILE_1);