Should be,
my $dataChunk = qx($cmd);
my @lines = split /[\r\s\v\V\n]+/, $dataChunk;
as system()
return value is the exit status of the program.
Вопрос
I have a file which I have to read chunk by chunk (based on time stamps). So i am using awk to read between two time intervals, and then I have to process the data further. For that I have to read the string I receive from awk Line By Line. When I perform Split using the new character, it doesn't split, but just all of the string remains in 1st field. I have used different escape characters but without any succes.
My main aim is to read the string line by line.
$cmd = "awk '\$0>=from&&\$0<=to' from=\"$currentTime\" to=\"$nextTime\" Output.txt";
$dataChunk = system ($cmd);
my @lines = split /[\r\s\v\V\n]+/, $dataChunk;
foreach my $line (@lines) {
chomp;
$timeStamp1 = (split /-/, $line)[1];
print "\n$timeStamp1\n";
exit;
}
Решение
Should be,
my $dataChunk = qx($cmd);
my @lines = split /[\r\s\v\V\n]+/, $dataChunk;
as system()
return value is the exit status of the program.
Другие советы
The problem is that system
doesn't do what you think it does. system
will return an exit code, not the command's standard output.
For the command's standard output, you want to use qx($cmd)
(or use the backtick quotes around the command).
Silly using awk
when you simply want to read between two timestamps:
use strict;
use warnings;
use autodie;
use feature qw(say);
use constant TIMESTAMP_FILE => 'Output.txt';
my $current_time = ...;
my $next_time = ...;
open my $tstamp_fh, "<", TIMESTAMP_FILE;
while ( my $line = <$tstamp_fh> ) {
chomp $line; # Always chomp on a read!
next if $line lt $current_time or $line gt $next_time;
say (split /-/, $line)[1]; # Is this the first or second element of the array?
}
Something like that. It's a bit hard to tell since I don't know what your file looks like.
The main thing is that there is absolutely no need to run awk
when you're using Perl. Whatever Awk can do, Perl can too.
You can always use the a2p
program that comes with Perl to translate any Awk program into Perl code if you're not 100 percent sure how to do something.