Re: How to parse UNIX commands ...



You can as well use perl at command line to evaluate UNIX command
output:

df -k | perl -ane 'print "$F[5]\n" if($F[5])'


T Baetzler wrote:
Hi,

benbart@xxxxxxxxxxxx asked:
How do I parse or evaluate the output of UNIX commands?

For example, in UNIX scripts, I can run filesystem=`df -k |
awk -F"" ' { print
$6 }'` to check for the mount points.

How do I achieve the same in Perl? That is, I tried:

Essentially in the same way - i.e. "my $x = `df -k`".

Maybe I can just simply run system 'df -k', re-direct that
into some output file and then parse it using OPEN-CLOSE
filehandles. Is there any easier way of doing this?

Yes, like this:

open( FH, '-|', 'df -k' ) or die "error: $!";

while( my $line = <FH> ){
# parse df output here
}

close( FH ) or die "error: $!";

I really recommend that you read the sections about open()
in the perlipc manpage. Lots of good stuff there!

HTH,
Thomas

.



Relevant Pages

  • RE: Timing several processes
    ... To clarify, I found the following in "Learning Perl". ... to parse the data I will generate, ... To unsubscribe, e-mail: beginners-unsubscribe@perl.org ...
    (perl.beginners)
  • RE: parsing very large tcpdump files
    ... dump, and it could do it in a matter of sections, ... If you need any perl pointers, ... Reconstruct all reconstructable sessions in an easy to parse way: ... Find out quickly and easily by testing it with real-world attacks from ...
    (Focus-IDS)
  • Re: Perl <-> C language interaction
    ... > Perl is just great to do the parse of the text file, ... there are regexp libs for C (even Perl compatible ... how can I interact between a perl script to a C program. ...
    (comp.lang.perl.misc)
  • Re: [PHP] Re: Scratch that
    ... (it's a reciprocal linking script) ... I also need to know how to run a expression-ish thing so that when I ... 'parse' the text, it returns all text between x and y, but parse it ... obviously perl is supported on your server. ...
    (php.general)
  • RE: monitoring corporate internet usage (fwd)
    ... Basically the code uses perl to parse the ... > Subject: RE: monitoring corporate internet usage ... > and front end it with a cgi perl web interface. ... I currently have my PIX racked and plugged in, ...
    (Security-Basics)