Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help: How to process output of a program

Reply
Thread Tools

Help: How to process output of a program

 
 
Amy Lee
Guest
Posts: n/a
 
      10-13-2008
Hello,

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

Thanks.

Amy
 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      10-13-2008
Amy Lee wrote:
> 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;
}


--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
 
Reply With Quote
 
 
 
 
Amy Lee
Guest
Posts: n/a
 
      10-13-2008
On Mon, 13 Oct 2008 17:03:37 +0200, Josef Moellers wrote:

> Amy Lee wrote:
>> 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;
> }

Really thanks.

Amy
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      10-13-2008
Amy Lee <> wrote:
>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
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      10-13-2008
Amy Lee <> wrote:
> On Mon, 13 Oct 2008 17:03:37 +0200, Josef Moellers wrote:
>
>> Amy Lee wrote:
>>> 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;
>> }

> 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?


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      10-14-2008
Amy Lee wrote:

> 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.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
grocery_stocker
Guest
Posts: n/a
 
      10-15-2008
On Oct 14, 2:12 pm, Tim Greer <t...@burlyhost.com> wrote:
> Amy Lee wrote:
> > 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.
> --


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.

 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      10-15-2008
grocery_stocker wrote:

> On Oct 14, 2:12 pm, Tim Greer <t...@burlyhost.com> wrote:
>> Amy Lee wrote:
>> > 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.
>> --

>
> 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.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      10-15-2008
Tim Greer <> wrote:
>
> 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.
 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      10-15-2008
wrote:

> Tim Greer <> wrote:
>>
>> 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)
>


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.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Can parellelized program run slower than single process program? Yesterday Paid Python 1 06-21-2012 05:58 AM
a simple program to illustrate that child process does not shareresources with parent process sree.harsha.sn@gmail.com C Programming 4 11-09-2008 03:59 AM
write a single line C program whose output is the program itself Puneet C Programming 16 03-20-2005 08:15 AM
(Win32) Timing out a process while reading process' output? rtm Perl 0 09-27-2004 10:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57