I'm trying to write a perl script which will read a file line by line, compare a particular substring of each line with that of the previously read line, and if different, writes it to a new file. In effect, script removes non-unique lines among sequential lines of a file.
The script seems to be suffering from a logical error, as instead of getting unique lines in the output file, I just get the terminating line.
use strict;
my $src='/var/www/pinginfo/ugvps';
my $tar="target";
open(INP, $src) or die("Could not open: $!.");
open(OUTP, ">", $tar) or die "Couldn't open: $!";
my $lastrd="";
while( my $line = <INP> ) {
if ( &IsSame($lastrd, $line)) {
print "Unique line: ".$line."\n";
print OUTP $line;
$lastrd=$line;
} else {
print "Line was the same: ".$line."\n";
}
}
print OUTP "Done";
close (OUTP);
close (INP);
exit 0;
sub IsSame {
my $old=$_[0];
my $new=$_[1];
if ( $old == "" ) {
return 0;
}
my @values_old = split('\|',$old);
my @values_new = split('\|',$new);
if ( $values_old[3] eq $values_new[3] ) {
#True - they are the same
return 1;
} else {
#False
return 0;
}
}
The file 'target' after execution, contains the single line Done
.
My source file looks like this:
UGVPS|6.6.6.6|03-08-2013 10:16:21 PM|0
UGVPS|6.6.6.6|03-08-2013 11:06:01 PM|0
UGVPS|6.6.6.6|03-08-2013 11:08:01 PM|100
UGVPS|6.6.6.6|03-08-2013 11:10:01 PM|0
UGVPS|6.6.6.6|03-08-2013 11:14:01 PM|100
UGVPS|6.6.6.6|03-08-2013 11:16:01 PM|0
UGVPS|6.6.6.6|03-08-2013 11:52:02 PM|0
UGVPS|6.6.6.6|03-08-2013 11:54:01 PM|100
UGVPS|6.6.6.6|03-08-2013 11:56:01 PM|100
UGVPS|6.6.6.6|03-08-2013 11:58:01 PM|100
UGVPS|6.6.6.6|04-08-2013 12:00:01 AM|0
UGVPS|6.6.6.6|04-08-2013 12:02:01 AM|100
UGVPS|6.6.6.6|04-08-2013 12:04:01 AM|100
UGVPS|6.6.6.6|04-08-2013 12:06:01 AM|100
UGVPS|6.6.6.6|04-08-2013 12:08:01 AM|100
UGVPS|6.6.6.6|04-08-2013 12:10:01 AM|100
UGVPS|6.6.6.6|04-08-2013 12:12:01 AM|100
UGVPS|6.6.6.6|04-08-2013 12:14:01 AM|100
UGVPS|6.6.6.6|04-08-2013 12:16:01 AM|100
UGVPS|6.6.6.6|04-08-2013 12:18:01 AM|100
UGVPS|6.6.6.6|04-08-2013 12:20:01 AM|100
UGVPS|6.6.6.6|04-08-2013 12:22:01 AM|100
UGVPS|6.6.6.6|04-08-2013 12:24:01 AM|0
UGVPS|6.6.6.6|04-08-2013 02:38:01 AM|0