Help: How to process output of a program

A

Amy Lee

Hello,

How to process output of a program? For example, I want to parse the
output of /sbin/lspci program.

Thanks.

Amy
 
J

Josef Moellers

Amy said:
Hello,

How to process output of a program? For example, I want to parse the
output of /sbin/lspci program.

if (open(my $lspci, '/sbin/lspci |')) {
while (<$lspci>) {
# process $_
}
close $lspci;
}
 
J

Jürgen Exner

Amy Lee said:
How to process output of a program? For example, I want to parse the
output of /sbin/lspci program.

You run the program and capture its output, using e.g. qx (aka
backticks) or open() into a pipe.

From the documentation:
qx/STRING/
`STRING`
A string which is [...] executed as a
system command with "/bin/sh" or its equivalent. [...] The
collected standard output of the command is returned;

open(): [...] if MODE is "'-|'", the filename
is interpreted as a command which pipes output to us.

Further details please see there.

On a side note: what kind of Perl tutorial/reference/documentation are
you using? You are asking _A_LOT_ of very beginner style questions,
which are typically covered quite early in any Perl tutorial or
reference book that I have seen. E.g. my copy of "Programming Perl"
explains backticks on page 52 out of over 600 pages, that is this topic
is covered within the first 10% of the book.

jue
 
T

Tad J McClellan

Amy Lee said:
Really thanks.


Shown above is one of the three ways of running an external
program from within Perl.

If you would like to know about all three of them, you can
start with:

perldoc -q external

How can I capture STDERR from an external command?
 
T

Tim Greer

Amy said:
Hello,

How to process output of a program? For example, I want to parse the
output of /sbin/lspci program.

Thanks.

Amy

Open with a pipe for read, or consider something like IPC::Open*. Read
the information about "Bidirectional Communication with Another
Process" in the docs.
 
G

grocery_stocker

Open with a pipe for read, or consider something like IPC::Open*. Read
the information about "Bidirectional Communication with Another
Process" in the docs.
--

I would just like to point out that the man pages for both IPC::Open2
and IPC::Open3 assume that the reader is somewhat proficient with
*nix. Over the course of the years, I've met a lot of perl
programmers that don't have the technical literacy to understand and
then use the IPC::Open* functions. I'm serious.
 
T

Tim Greer

grocery_stocker said:
I would just like to point out that the man pages for both IPC::Open2
and IPC::Open3 assume that the reader is somewhat proficient with
*nix. Over the course of the years, I've met a lot of perl
programmers that don't have the technical literacy to understand and
then use the IPC::Open* functions. I'm serious.

I don't doubt what you say. Being a Perl programmer doesn't make you a
systems administrator (especially a *nix sys admin), and there are
chances of some people not understanding the suggestion. However, I
feel it is best to offer the suggestions just in case, and that's
basically always the case).

Of course, there's no denying that most people that claim to be Perl
programmers are hardly people you would feel comfortable using the term
"Programmer" to describe anyway, so I am aware that pretty much any
suggestion could equally be best left unsaid or tossed out the window,
but I like to offer everyone the same chances, even if sometimes it
might be in vain.

You may notice when I post on usenet, that I rarely to never go into any
great detail, and that's primarily the reason why (going on about
something too unfamiliar to the OP). Should someone wish to pay me to
code for them that's different, but on usenet, I try to post in a frame
of mind to provide everyone with the same answers, so they can improve
if they are capable and willing.

Also, IPC::Open2 does work on Win32 or am I mistaken? (IPC::Open3 isn't
fully supported on it though), and regardless, if the user read over
the module's documentation, they could get some insight/ideas, even
with the pipe usage it could replace.

Also, I didn't want to assume their platform, but the fact they said the
wanted the output from /sbin/lspci, pretty much sealed the suggestion
in my mind and had me believe they have some type of insight. Further
suggestions, directions and information, even if they don't use it, is
always a good thing. They might be able to benefit from it, but it
could certainly only add confusion.
 
X

xhoster

Tim Greer said:
Also, IPC::Open2 does work on Win32 or am I mistaken?

Based on my test, I'd say it doesn't work on Win32, or at least not
well.

use strict;
use warnings;
use IPC::Open2;
$|=1;
warn open2 my $in, my $out, q{sort};
print $out qq{$_\n} foreach (1..100);
close $out or die;
print while (<$in>);

This works on Linux, but hangs on Windows. It hangs at the while (<$in>).
Since sort needs to read all input before generating any output,
deadlock should not be an issue. It also leaves sort.exe hanging around.

(I actually starting using "cat"/"type", using the >& to redirect to
another filehandle to avoid deadlocks, but I couldn't figure out how to
make Window's "type" read from its stdin. So I switched to sort. Then
once using sort, I realized I no longer needed >& so simplified it. None
of these steps worked on windows).

This is perl, v5.8.8 built for MSWin32-x86-multi-thread
(with 25 registered patches, see perl -V for more detail)

Copyright 1987-2006, Larry Wall

Binary build 817 [257965] provided by ActiveState
http://www.ActiveState.com Built Mar 20 2006 17:54:25

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
T

Tim Greer

Based on my test, I'd say it doesn't work on Win32, or at least not
well.

use strict;
use warnings;
use IPC::Open2;
$|=1;
warn open2 my $in, my $out, q{sort};
print $out qq{$_\n} foreach (1..100);
close $out or die;
print while (<$in>);

This works on Linux, but hangs on Windows. It hangs at the while
(<$in>). Since sort needs to read all input before generating any
output,
deadlock should not be an issue. It also leaves sort.exe hanging
around.

(I actually starting using "cat"/"type", using the >& to redirect to
another filehandle to avoid deadlocks, but I couldn't figure out how
to
make Window's "type" read from its stdin. So I switched to sort.
Then
once using sort, I realized I no longer needed >& so simplified it.
None of these steps worked on windows).

This is perl, v5.8.8 built for MSWin32-x86-multi-thread
(with 25 registered patches, see perl -V for more detail)

Interesting. I've honestly not tried this on Win32. I've been
privileged enough to _not_ have to use Windows for many, many years.
Maybe I'll install ActiveState's Perl on a Windows laptop and run some
tests (but I probably won't). I appreciate the follow up.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top