6

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
4

1 回答 1

5

好的,我做到了:

#!/usr/bin/perl

use strict;
use warnings;

my $src='./input.txt';
my $tar="./target.txt";
open(INP, $src) or die("Could not open: $!.");
open(OUTP, ">", $tar) or die "Couldn't open: $!";
my $lastrd="";
while( my $line = <INP> ) {
    unless ( 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 eq "" ) {
        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;
    }
}

印刷:

UGVPS|6.6.6.6|03-08-2013 10:16:21 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:54: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:24:01 AM|0
Done

如果您在代码中启用警告,您会看到有用的信息:

Argument "" isn't numeric in numeric eq (==) at 7.pl line 28, <INP> line 2.
Argument "" isn't numeric in numeric eq (==) at 7.pl line 28, <INP> line 3.

这可以帮助您找到==错误。

if ( &IsSame($lastrd, $line))我也改成unless ( IsSame($lastrd, $line))

于 2013-08-04T10:29:13.073 回答