RE: How to parse UNIX commands ...



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: File writes in a loop
    ... Here the un-vague version of the question. ... We've decided for business reasons to parse it by ... I won't know the date, which is the name of the output file, ... All this works fine until it does the Open/Create Output on the second ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: File writes in a loop
    ... We've decided for business reasons to parse it by ... I won't know the date, which is the name of the output file, ... performance hit of closing and opening the same file thousands of ... All this works fine until it does the Open/Create Output on the second ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: I love csv format
    ... It' readable and even editable in vim or nano. ... parse and rearrange with awk. ...
    (comp.programming)
  • Re: easiest way to add first line to file
    ... Above works fine but input and output file should not be the same. ... to modify file without temporary files? ... awk 'BEGIN ...
    (comp.unix.shell)
  • Re: How to read and get the string from fixed position?
    ... I'm starting using shell script, ... generate output file with the value ... Use substrand don't use a file for the above but instead just save the "value"s in an array on your first pass, then test for the selected string being in the array on the second pass, e.g.: ... Note that "nextfile" is a GNU awk extension. ...
    (comp.unix.shell)