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

Re: Storing system grep's output to an array in perl

by rnewsham (Curate)
on Mar 07, 2013 at 11:41 UTC ( [id://1022213]=note: print w/replies, xml ) Need Help??


in reply to Storing system grep's output to an array in perl

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;

Replies are listed 'Best First'.
Re^2: Storing system grep's output to an array in perl
by SuicideJunkie (Vicar) on Mar 07, 2013 at 19:03 UTC

    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; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1022213]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-23 22:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found