0

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;
    }
Muz
  • 73
  • 1
  • 8
  • Just to be sure, if you print the line does it print on several lines? Or on a single line? – AntonH May 01 '14 at 20:35
  • It prints on several lines, when I do 'more Output.txt' – Muz May 01 '14 at 20:36
  • 1
    Are you sure you want to use `system`? Shouldn't you use `qx($cmd)`? – Platinum Azure May 01 '14 at 20:36
  • Sorry its a mistake while pasting the code here, its $dataChunk in real code. – Muz May 01 '14 at 20:37
  • For informations sake: http://stackoverflow.com/questions/799968/whats-the-difference-between-perls-backticks-system-and-exec – AntonH May 01 '14 at 20:41
  • 1
    Perl was written in part as an `awk`-killer (not 100% successfully since `awk` is still around). However, if you run `awk` from Perl and then process its output, you are 'doing it wrong' — Perl can manage what `awk` does on its own. – Jonathan Leffler May 01 '14 at 20:47
  • 1
    You can also avoid all the backslashes in the command assignment by using custom single quotes: `$cmd = q[awk '$0>=from&&$0<=to' from="$currentTime" to="$nextTime" Output.txt];` — where the `q[…]` is one possible custom quotes (bracketing pairs), or you could use a non-bracketing character like `q%…%` for any convenient character that doesn't appear in the body. You can use custom double quotes too: `qq[…]` or `qq%…%` etc. – Jonathan Leffler May 01 '14 at 20:53

3 Answers3

6

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.

mpapec
  • 50,217
  • 8
  • 67
  • 127
4

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).

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
2

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.

David W.
  • 105,218
  • 39
  • 216
  • 337