Extraction Fields from a file

D

doni

I wanted to extract the 3rd field from every 2nd and 3rd line after
spaces from a file. This is how the information is stored in the file.

00009c8 15 106
00009c4 15 114
00009c8 15 119

00009c8 15 108
00009c4 15 114
00009c8 15 119

00009c8 15 109
00009c4 15 114
00009c8 15 119

00009c8 15 109
00009c4 15 114
00009c8 15 119

00009c8 15 106
00009c4 15 114
00009c8 15 119

00009c8 15 106
00009c4 15 114
00009c8 15 119

00009c8 15 106
00009c4 15 114
00009c8 15 119

00009c8 15 107
00009c4 15 114
00009c8 15 119

Here is the script that I have written to approach the problem. The
script is not a complete one.

#! /usr/bin/perl -w

$ex_data = "extract_data";

# Open the rfping data file #
open(DAT,$ex_data) || die("Cannot Open File");

foreach my $line(<DAT>)
{
chomp $line;
my @data = split(/\s+/,$line);
my @rssi_09c8_09c4;
if ($data[0] eq "00009c4")
{
@rssi_09c8_09c4 = $data[2];
print "The rssi value is: @rssi_09c8_09c4\n";
}
else
{

}
}
close(DAT);

The other problem is when I run this script, I am getting "Use of
uninitialized value at ./rfping.pl line 13," warning message.
Can anyone let me know how should I proceed with this.

Thanks,
doni
 
J

John Bokma

doni said:
00009c8 15 107
00009c4 15 114
00009c8 15 119
[..]

#! /usr/bin/perl -w

Don't use -w, use:

use strict;
use warnings;
$ex_data = "extract_data";

my $ex_data = ....
# Open the rfping data file #
open(DAT,$ex_data) || die("Cannot Open File");

open my $fh, $ex_data or die "Can't open '$ex_data' for reading: $!";

learn why I write it like this, especially the die part.
foreach my $line(<DAT>)
{

while ( my $line = said:
}
close(DAT);

close $fh or die "Can't close '$ex_data' after reading: $!";
The other problem is when I run this script, I am getting "Use of
uninitialized value at ./rfping.pl line 13," warning message.
Can anyone let me know how should I proceed with this.

Yup, always wonderful if you mark line 13. I am not going to count for
you.

Probably caused by empty lines.

while ( my $line = <$fh> ) {

my ( $rssi ) = $line =~ /^[0-9a-f]{7} /;
defined $rssi or next;
$rssi eq '00009c4' or next;

print "The rssi value is: $rrsi\n";
}
 
U

usenet

doni said:
I wanted to extract the 3rd field from every 2nd and 3rd line after
spaces from a file.

my $linecount;
while (<DATA>) {
$linecount = (/^\s*$/) ? 0 : $linecount + 1; #reset count if
empty
if ($linecount == 2 || $linecount == 3) {
print ( (split)[2] . "\n" ); #third field
}
}
 
C

Craig

doni said:
#! /usr/bin/perl -w

$ex_data = "extract_data";

# Open the rfping data file #
open(DAT,$ex_data) || die("Cannot Open File");

foreach my $line(<DAT>)
{
chomp $line;
my @data = split(/\s+/,$line);
my @rssi_09c8_09c4;
if ($data[0] eq "00009c4")
{
@rssi_09c8_09c4 = $data[2];
print "The rssi value is: @rssi_09c8_09c4\n";
}
else
{

}
}
close(DAT);

#! /usr/bin/perl
use warnings;
use strict;

my $ex_data = "extract_data";
my $fh;

open($fh,$ex_data) or die("Can not open file $ex_data", $?, $!);
my @result;
my $match = "00009c4";
while(<$fh>)> {
next unless /^$match\s+\d+\s+(\d+)\s+/;
push @result, $1;
}
for(@result) { print $fh $_, "\n"; }
close($fh);
 
J

John W. Krahn

doni said:
I wanted to extract the 3rd field from every 2nd and 3rd line after
spaces from a file. This is how the information is stored in the file.

00009c8 15 106
00009c4 15 114
00009c8 15 119

00009c8 15 108
00009c4 15 114
00009c8 15 119

00009c8 15 109
00009c4 15 114
00009c8 15 119

00009c8 15 109
00009c4 15 114
00009c8 15 119

00009c8 15 106
00009c4 15 114
00009c8 15 119

00009c8 15 106
00009c4 15 114
00009c8 15 119

00009c8 15 106
00009c4 15 114
00009c8 15 119

00009c8 15 107
00009c4 15 114
00009c8 15 119

Here is the script that I have written to approach the problem. The
script is not a complete one.

#! /usr/bin/perl -w

$ex_data = "extract_data";

# Open the rfping data file #
open(DAT,$ex_data) || die("Cannot Open File");

foreach my $line(<DAT>)
{
chomp $line;
my @data = split(/\s+/,$line);
my @rssi_09c8_09c4;
if ($data[0] eq "00009c4")
{
@rssi_09c8_09c4 = $data[2];
print "The rssi value is: @rssi_09c8_09c4\n";
}
else
{

}
}
close(DAT);

#!/usr/bin/perl
use warnings;
use strict;

my $ex_data = 'extract_data';

# Open the rfping data file #
open my $DAT, '<', $ex_data or die "Cannot Open '$ex_data' $!";

$/ = '';
my @rssi_09c8_09c4;
while ( <$DAT> ) {
push @rssi_09c8_09c4, ( /(\d+)$/gm )[ -2, -1 ];
}

close $DAT;

__END__




John
 
D

DJ Stunks

doni said:
I wanted to extract the 3rd field from every 2nd and 3rd line after
spaces from a file. This is how the information is stored in the file.

I'd go three dimensional, man.

#!/usr/bin/perl

use strict;
use warnings;

use English qw{ -no_match_vars };
$INPUT_RECORD_SEPARATOR = ''; #paragraph mode

my @data;
while ( my $record = <DATA> ) {
my @lines = split /\n/, $record;
$_ = [split] for @lines;
push @data, \@lines;
}

print "2nd & 3rd fields of the 2nd row of the 4th record: ";
for my $record ( 4 ) {
for my $row ( 2 ) {
for my $field ( 2,3 ) {
print $data[ $record-1 ][ $row-1 ][ $field-1 ];
print ' ';
}
}
}
print "\n";

__DATA__
00009c8 15 106
00009c4 15 114
00009c8 15 119

00009c8 15 108
00009c4 15 114
00009c8 15 119

00009c8 15 109
00009c4 15 114
00009c8 15 119

00009c8 15 109
00009c4 15 114
00009c8 15 119

00009c8 15 106
00009c4 15 114
00009c8 15 119

00009c8 15 106
00009c4 15 114
00009c8 15 119

00009c8 15 106
00009c4 15 114
00009c8 15 119

00009c8 15 107
00009c4 15 114
00009c8 15 119

-jp
 
J

John W. Krahn

Craig said:
#! /usr/bin/perl
use warnings;
use strict;

my $ex_data = "extract_data";
my $fh;

open($fh,$ex_data) or die("Can not open file $ex_data", $?, $!);
my @result;
my $match = "00009c4";
while(<$fh>)> {
next unless /^$match\s+\d+\s+(\d+)\s+/;
push @result, $1;
}
for(@result) { print $fh $_, "\n"; }

You can't print to the $fh filehandle because it was opened readonly.
close($fh);


John
 
U

usenet

doni said:
I wanted to extract the 3rd field from every 2nd and 3rd line after
spaces from a file.

my $linecount;
while (<DATA>) {
$linecount = (/^\s*$/) ? 0 : $linecount + 1; #reset count if
empty
if ($linecount == 2 || $linecount == 3) {
print ( (split)[2] . "\n" ); #third field
}
}
 
D

doni

thanks everyone for replying to my question.
I tried David's hint and it worked perfectly for me.

Thanks,
doni

doni said:
I wanted to extract the 3rd field from every 2nd and 3rd line after
spaces from a file.

my $linecount;
while (<DATA>) {
$linecount = (/^\s*$/) ? 0 : $linecount + 1; #reset count if
empty
if ($linecount == 2 || $linecount == 3) {
print ( (split)[2] . "\n" ); #third field
}
}
 
J

John W. Krahn

doni said:
I wanted to extract the 3rd field from every 2nd and 3rd line after
spaces from a file.

my $linecount;
while (<DATA>) {
$linecount = (/^\s*$/) ? 0 : $linecount + 1; #reset count if
empty
if ($linecount == 2 || $linecount == 3) {
print ( (split)[2] . "\n" ); #third field
}
}

while ( <DATA> ) {
$. = 0 unless /\S/;
print( (split)[2], "\n" ) if 2 .. 3;
}



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top