D
doni
Hi,
I was wondering how can I read two lines from a file at a time and
extract the data from the first line and second line onto different
variables.
Below is the data snippet that I am using to write my program. The
data contains the path information extracted from each node. The node
value which occurs at the beginning of the next line after "from" line
is the one whose path information we are extracting.
If the line does not contain "from" field at the beginning, I want to
read 2 lines and extract the contents of the first line and second
line onto different fields before we iterate through the while loop .
If the line contains "from" field read the line and iterate through
the while loop to read the next line from the file.
from to reachable rssi
08:c0 09:c3 yes 3
09:c3 08:c0 yes 2
08:c0 09:c4 yes 7
09:c4 08:c0 yes 7
08:c0 09:c8 yes 3
09:c8 08:c0 yes 3
08:c0 09:d8 no 8
09:d8 08:c0 no 7
from to reachable rssi
09:c3 08:22 yes 15
08:22 09:c3 yes 10
09:c3 08:c0 yes 2
08:c0 09:c3 yes 3
09:c3 09:c4 no 4
09:c4 09:c3 no 8
from to reachable rssi
09:c4 08:c0 yes 7
08:c0 09:c4 yes 7
09:c4 09:c3 no 8
09:c3 09:c4 no 4
09:c4 09:c8 yes 7
09:c8 09:c4 yes 7
from to reachable rssi
09:c8 08:c0 yes 3
08:c0 09:c8 yes 3
09:c8 09:c4 yes 7
09:c4 09:c8 yes 7
09:c8 09:d8 yes 6
09:d8 09:c8 yes 6
from to reachable rssi
09:d8 08:22 yes 4
08:22 09:d8 yes 7
09:d8 08:c0 no 7
08:c0 09:d8 no 8
09:d8 09:c8 yes 6
09:c8 09:d8 yes 6
Here is the code I wrote to perform the above operation.
#! /usr/bin/perl
use strict;
use warnings;
my $ex_data = 'routed_info';
open (NETSTAT,$ex_data) || die("Cannot Open File: $!");
my $route_node; my $rssi;
my @mac_id;
my $first_node; my $second_node;
my $first_rssi; my $second_rssi;
my $first_link; my $second_link;
while (<NETSTAT>) {
chomp;
my @words = split;
if (/^([a-z]+)\s+([a-z]+)\s+([a-z]+)\s+([a-z]+)/) {
$route_node = 1;
}
elsif ($route_node == 1) {
push (@mac_id, $words[0]);
$route_node = 0;
$first_node = $words[1]; $first_link = $words[2]; $first_rssi
= $words[3];
next <NETSTAT>;
$second_link = $words[2]; $second_rssi = $words[3];
if (($first_link eq "yes") && ($second_link eq "yes")) {
$rssi = ($first_rssi + $second_rssi)/2;
print "Combined RSSI value from $words[1] and
$first_node is: $rssi\n";
}
}
else {
}
}
close(NETSTAT) || die("Cannot close $ex_data: $!");
Pls, let me know If I I havent clearly described my issue here. Can
anyone let me know how can I perform this.
Thanks,
doni
I was wondering how can I read two lines from a file at a time and
extract the data from the first line and second line onto different
variables.
Below is the data snippet that I am using to write my program. The
data contains the path information extracted from each node. The node
value which occurs at the beginning of the next line after "from" line
is the one whose path information we are extracting.
If the line does not contain "from" field at the beginning, I want to
read 2 lines and extract the contents of the first line and second
line onto different fields before we iterate through the while loop .
If the line contains "from" field read the line and iterate through
the while loop to read the next line from the file.
from to reachable rssi
08:c0 09:c3 yes 3
09:c3 08:c0 yes 2
08:c0 09:c4 yes 7
09:c4 08:c0 yes 7
08:c0 09:c8 yes 3
09:c8 08:c0 yes 3
08:c0 09:d8 no 8
09:d8 08:c0 no 7
from to reachable rssi
09:c3 08:22 yes 15
08:22 09:c3 yes 10
09:c3 08:c0 yes 2
08:c0 09:c3 yes 3
09:c3 09:c4 no 4
09:c4 09:c3 no 8
from to reachable rssi
09:c4 08:c0 yes 7
08:c0 09:c4 yes 7
09:c4 09:c3 no 8
09:c3 09:c4 no 4
09:c4 09:c8 yes 7
09:c8 09:c4 yes 7
from to reachable rssi
09:c8 08:c0 yes 3
08:c0 09:c8 yes 3
09:c8 09:c4 yes 7
09:c4 09:c8 yes 7
09:c8 09:d8 yes 6
09:d8 09:c8 yes 6
from to reachable rssi
09:d8 08:22 yes 4
08:22 09:d8 yes 7
09:d8 08:c0 no 7
08:c0 09:d8 no 8
09:d8 09:c8 yes 6
09:c8 09:d8 yes 6
Here is the code I wrote to perform the above operation.
#! /usr/bin/perl
use strict;
use warnings;
my $ex_data = 'routed_info';
open (NETSTAT,$ex_data) || die("Cannot Open File: $!");
my $route_node; my $rssi;
my @mac_id;
my $first_node; my $second_node;
my $first_rssi; my $second_rssi;
my $first_link; my $second_link;
while (<NETSTAT>) {
chomp;
my @words = split;
if (/^([a-z]+)\s+([a-z]+)\s+([a-z]+)\s+([a-z]+)/) {
$route_node = 1;
}
elsif ($route_node == 1) {
push (@mac_id, $words[0]);
$route_node = 0;
$first_node = $words[1]; $first_link = $words[2]; $first_rssi
= $words[3];
next <NETSTAT>;
$second_link = $words[2]; $second_rssi = $words[3];
if (($first_link eq "yes") && ($second_link eq "yes")) {
$rssi = ($first_rssi + $second_rssi)/2;
print "Combined RSSI value from $words[1] and
$first_node is: $rssi\n";
}
}
else {
}
}
close(NETSTAT) || die("Cannot close $ex_data: $!");
Pls, let me know If I I havent clearly described my issue here. Can
anyone let me know how can I perform this.
Thanks,
doni