LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-02-2013, 12:35 AM   #1
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Rep: Reputation: Disabled
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.
 
Old 01-02-2013, 09:07 AM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
What have you written so far?
 
Old 01-02-2013, 09:22 AM   #3
bijo505
Member
 
Registered: Nov 2012
Location: Bangalore
Distribution: Fedora & Ubuntu
Posts: 77

Rep: Reputation: 18
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
 
Old 01-02-2013, 06:56 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
1 members found this post helpful.
Old 01-03-2013, 12:25 AM   #5
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by eminempark View Post
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
 
Old 01-03-2013, 12:26 AM   #6
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by theNbomr View Post
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...
 
Old 01-03-2013, 12:29 AM   #7
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
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...
 
Old 01-03-2013, 11:50 AM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 01-03-2013, 08:02 PM   #9
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by theNbomr View Post
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..
 
Old 01-03-2013, 08:22 PM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 01-03-2013, 08:40 PM   #11
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by theNbomr View Post
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...
 
  


Reply

Tags
lines, perlscript



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to compare 2 tables data from different linux servers with mysql using perl script tarun28jain Programming 3 10-16-2012 06:54 AM
[SOLVED] script to read and compare two consecutive lines in a file smritisingh03 Linux - Newbie 16 08-28-2012 04:36 AM
[SOLVED] Requesting Assistance with Perl script to compare two files pspreben Linux - Software 11 09-09-2011 12:41 AM
need help with awk-script (compare two lines) Mauline Programming 2 11-27-2008 04:12 AM
NEED HELP IN comment lines PERL Perl script adam_blackice Programming 17 11-07-2007 08:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration