Friday, 15 May 2015

perl Tie::File to remove lines in text file -



perl Tie::File to remove lines in text file -

i have next code remove lines out of text file using. however, not removing lines want. have done lot of testing here , confirmed remove lines if line number right. however, $. seems duplicating actual line number (as when print on it, says there double number of lines there are!)

#!/usr/bin/perl # find_company_codes.pl utilize strict; utilize warnings; utilize diagnostics; utilize tie::file; $split_file = "//server/folder/splits_table.txt"; # remove fields mas has rollcall @file; @lines; tie @file, 'tie::file', $split_file or die $_; foreach (@file) { @lines = split(",",$_); next if $lines[0] eq "esdb link processing reg-prd-ccd"; next if $lines[2] ne ""; next if $lines[3] ne ""; next if $lines[4] ne ""; next if $lines[5] ne ""; next if $lines[6] ne ""; splice(@file, $.,1); } untie @file;

p.s. - know can utilize this, however, thought using 'tie::file` little more straight point in case.

you're not reading file, you're iterating on array. $. makes no sense. first, need switch indexes.

for (0..$#file) { @fields = split(/,/, $file[$_]); next if $fields[0] eq "esdb link processing reg-prd-ccd"; next if $fields[2] ne ""; next if $fields[3] ne ""; next if $fields[4] ne ""; next if $fields[5] ne ""; next if $fields[6] ne ""; splice(@file, $_, 1); }

except doesn't work either. when splice, shift entire array, must not advance index turn, , need finish loop 1 index sooner. fix:

my $i = 0; while ($i < $#file) { @fields = split(/,/, $file[$i]); if ( $fields[0] ne "esdb link processing reg-prd-ccd" && $fields[2] eq "" && $fields[3] eq "" && $fields[4] eq "" && $fields[5] eq "" && $fields[6] eq "" ) { splice(@file, $i, 1); } else { ++$i; } }

good, works, it's crazy slow. every splice can cause rest of file read , rewritten. yet inappropriate utilize of tie::file! next incredibly faster.

use strict; utilize warnings; $qfn = "//rosefs19w/sal..."; @file; { open(my $fh, '<', $qfn) or die("can't open $qfn: $!\n"); @file = <$fh>; } $i = 0; while ($i < $#file) { @fields = split(/,/, $file[$i]); if ( $fields[0] ne "esdb link processing reg-prd-ccd" && $fields[2] eq "" && $fields[3] eq "" && $fields[4] eq "" && $fields[5] eq "" && $fields[6] eq "" ) { splice(@file, $i, 1); } else { ++$i; } } { open(my $fh, '>', $qfn) or die("can't create $qfn: $!\n"); print $fh @file; }

that's right. add together 3 lines of code , remove 2, , code becomes 100, 1000, 10000 times faster!

perl

No comments:

Post a Comment