Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Storing system grep's output to an array in perl

by tc_blr (Novice)
on Mar 07, 2013 at 11:12 UTC ( #1022200=perlquestion: print w/ replies, xml ) Need Help??
tc_blr has asked for the wisdom of the Perl Monks concerning the following question:

How can i store the output of a system grep command in a perl array?

I am supposed to read a file that has a lot of lines. Few lines start with top - 00:35:18 up... I need to store all the occurances of times like 00:35:18 in a n array.

There are also a few more places where times like these are present. But i need only those occurances that start with top - 00:35:18 up...

The grep that I am currently using is grep -i "top-" filename | awk '(print $3)'

Comment on Storing system grep's output to an array in perl
Select or Download Code
Re: Storing system grep's output to an array in perl
by vinoth.ree (Parson) on Mar 07, 2013 at 11:32 UTC

    system() executes the command specified but it doesn't capture the output of the command.

    system() will return the exit status of the command as returned by the 'wait' call.

    Update:

    Use backtick(``) to capture the specified command's output.

    my @array = `grep -i "top-" filename | awk '(print $3)'`;
Re: Storing system grep's output to an array in perl (perlfaq8)
by Anonymous Monk on Mar 07, 2013 at 11:36 UTC
Re: Storing system grep's output to an array in perl
by rnewsham (Hermit) on Mar 07, 2013 at 11:41 UTC

    The horrible way, which I do not recommend, would be to shell out with backticks

    my @tops = `/bin/grep -i "top-" filename | /usr/bin/awk '(print $3)'`;

    A better way would be something like

    my @tops; open FILE, "<filename" or die "could not open filename"; while ( <FILE> ) { if ( m/^top \- (\d{2}:\d{2}:\d{2})/ ) { push @tops, $1; } } close FILE;

      Or even better:

      my @tops; open my $inputFileHandle, '<', $filename or die "could not open '$file +name', because the OS said: $!"; while ( <$inputFileHandle> ) { if ( m/^top \- (\d{2}:\d{2}:\d{2})/ ) { push @tops, $1; } }
Re: Storing system grep's output to an array in perl
by arnaud99 (Beadle) on Mar 07, 2013 at 11:52 UTC

    Hi

    A similar approach to the one above

    use strict; use warnings; my @all_times; while (my $line = <DATA>) { if ($line =~ /^top - /) { chomp $line; my ($top, $dash, $time, @rest) = split /\s+/, $line; push @all_times, $time; } } for (@all_times) { print $_, "\n"; } __DATA__ top - 00:35:18 up... blah blah blah ..... top - 00:35:15 up... fwertwe blah blah blah ..... blah blah blah ..... blah blah blah ..... blah blah blah ..... blah blah blah ..... top - 00:35:20 up... blah blah blah ..... top - 00:35:22 up... blah blah blah ..... top - 00:35:30 up... blah blah blah .....

    Output is:

    00:35:18 00:35:15 00:35:20 00:35:22 00:35:30

    Arnaud

Re: Storing system grep's output to an array in perl
by rjt (Deacon) on Mar 07, 2013 at 12:26 UTC

    The pure Perl solution will be faster, more portable, and much sexier (substitute your actual filehandle opened with open for <DATA>):

    #!/usr/bin/env perl use 5.010; use warnings; my @times = map { /^(?:top - (..:..:..))/ } <DATA>; say for @times; __DATA__ top - 00:35:18 up top - 01:23:45 other junk completely different format altogether not - 01:23:45 can't fool me! top - 02:35:46 top - 03:46:07

    Output:

    00:35:18 01:23:45 02:35:46 03:46:07
Re: Storing system grep's output to an array in perl
by Anonymous Monk on Mar 12, 2013 at 10:59 UTC
    Thank u monks...
Re: Storing system grep's output to an array in perl
by Anonymous Monk on Mar 12, 2013 at 11:01 UTC
    Thnks a lot monks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1022200]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2014-07-29 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls