LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl script to continuously compare value from lines (https://www.linuxquestions.org/questions/programming-9/perl-script-to-continuously-compare-value-from-lines-4175443758/)

eminempark 01-02-2013 12:35 AM

Perl script to continuously compare value from lines
 
I have a file called "data.txt". And inside the data file, I have briefly
Code:

PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 3 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28
PacketID 6 temperature 28

May I know the perl script that I need to write so the result will be:
Code:

PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28

The perl script will eliminated the PacketID 3 because PacketID 2 and PacketID 3 have the same value. Same goes to PacketID 6, because it has the same value with PacketID 5.

Habitual 01-02-2013 09:07 AM

What have you written so far?

bijo505 01-02-2013 09:22 AM

Hi Eminempark,

you can achieve this with the help of bash command it self

Code:

$ cat data.txt | sort -t " "  -k 4 -nu
PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28

if you want to use perl, you can use the following

Code:

perl -e 'system "(cat data.txt | sort -t \" \" -k 4 -un)"';
PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28

--
Thanks,
Bijo

theNbomr 01-02-2013 06:56 PM

So, your algorithm is intended to eliminate all records that are not unique in the second column, or just the ones that are consecutive with the record(s) immediately preceding, or.... It is impossible to know without some explanation. In part, I ask because I want to coerce you to think about the problem so that you will better learn how to solve them for yourself, or at the very least, how to compose a question about a problem.

--- rod.

eminempark 01-03-2013 12:25 AM

Quote:

Originally Posted by eminempark (Post 4861031)
I have a file called "data.txt". And inside the data file, I have briefly
Code:

PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 3 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28
PacketID 6 temperature 28

May I know the perl script that I need to write so the result will be:
Code:

PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28

The perl script will eliminated the PacketID 3 because PacketID 2 and PacketID 3 have the same value. Same goes to PacketID 6, because it has the same value with PacketID 5.

Code:

$id = 0;

open FILE, "id.txt" or die $!;
while (my $line = <FILE>) {
 ($a1, $a2) = split ' ', $line;
 ($b1, $b2) = split ' ', $line;
 if ($a2 eq $b2) {
  print "$a1 $a2\n";
  $id = $a1;
 }
}
close(FILE);

But, I think the a and b variable is wrong

eminempark 01-03-2013 12:26 AM

Quote:

Originally Posted by theNbomr (Post 4861674)
So, your algorithm is intended to eliminate all records that are not unique in the second column, or just the ones that are consecutive with the record(s) immediately preceding, or.... It is impossible to know without some explanation. In part, I ask because I want to coerce you to think about the problem so that you will better learn how to solve them for yourself, or at the very least, how to compose a question about a problem.

--- rod.

I want to eliminate the consecutive record(s). Any idea? really need some helps...

eminempark 01-03-2013 12:29 AM

Quote:

Originally Posted by Habitual (Post 4861267)
What have you written so far?

Code:

$id = 0;

open FILE, "id.txt" or die $!;
while (my $line = <FILE>) {
 ($a1, $a2) = split ' ', $line;
 ($b1, $b2) = split ' ', $line;
 if ($a2 eq $b2) {
  print "$a1 $a2\n";
  $id = $a1;
 }
}
close(FILE);

But I think the variable a and b are wrong...

theNbomr 01-03-2013 11:50 AM

You're on the right track...
Code:


#! /usr/bin/perl -w
#
#  LQeminempark.pl
#
#  Usage: LQeminempark.pl datafile
#
#
use strict;

my $lastval = -32768;
while(<>){

    my( $tag1, $val1, $tag2, $val2 ) = split;
    if( $lastval != $val2 ){
        print $_;
    }
    $lastval = $val2;
}

--- rod.

eminempark 01-03-2013 08:02 PM

Quote:

Originally Posted by theNbomr (Post 4862226)
You're on the right track...
Code:


#! /usr/bin/perl -w
#
#  LQeminempark.pl
#
#  Usage: LQeminempark.pl datafile
#
#
use strict;

my $lastval = -32768;
while(<>){

    my( $tag1, $val1, $tag2, $val2 ) = split;
    if( $lastval != $val2 ){
        print $_;
    }
    $lastval = $val2;
}

--- rod.

I have run this code but it still gives me the repeated value. It doesn't eliminate any value..

theNbomr 01-03-2013 08:22 PM

It works for me:
Code:


$ cat LQeminempark.dat
PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 3 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28
PacketID 6 temperature 28

$ ./LQeminempark.pl LQeminempark.dat
PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28

Is your sample data accurately transcribed? What happens if you copy/paste back to a datafile from the LQ posting? Maybe there is some invisible data being lost between you and LQ. Are you sure your copy of the program was accurately transcribed? Looking back at the code, I don't see anything that looks like an assumption or other trap. Maybe someone else can pinpoint a problem.

--- rod.

eminempark 01-03-2013 08:40 PM

Quote:

Originally Posted by theNbomr (Post 4862509)
It works for me:
Code:


$ cat LQeminempark.dat
PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 3 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28
PacketID 6 temperature 28

$ ./LQeminempark.pl LQeminempark.dat
PacketID 1 temperature 25
PacketID 2 temperature 26
PacketID 4 temperature 27
PacketID 5 temperature 28

Is your sample data accurately transcribed? What happens if you copy/paste back to a datafile from the LQ posting? Maybe there is some invisible data being lost between you and LQ. Are you sure your copy of the program was accurately transcribed? Looking back at the code, I don't see anything that looks like an assumption or other trap. Maybe someone else can pinpoint a problem.

--- rod.

YEAH!!! it is working now. My command to execute it was wrong. Thank you everyone especially theNbomr...


All times are GMT -5. The time now is 08:06 AM.