Help: How to process output of a program

Discussion in 'Perl Misc' started by Amy Lee, Oct 13, 2008.

  1. Amy Lee

    Amy Lee Guest

    Hello,

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

    Thanks.

    Amy
    Amy Lee, Oct 13, 2008
    #1
    1. Advertising

  2. 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
    Josef Moellers, Oct 13, 2008
    #2
    1. Advertising

  3. Amy Lee

    Amy Lee Guest

    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
    Amy Lee, Oct 13, 2008
    #3
  4. 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
    Jürgen Exner, Oct 13, 2008
    #4
  5. 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/"
    Tad J McClellan, Oct 13, 2008
    #5
  6. Amy Lee

    Tim Greer Guest

    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!
    Tim Greer, Oct 14, 2008
    #6
  7. On Oct 14, 2:12 pm, Tim Greer <> 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.
    grocery_stocker, Oct 15, 2008
    #7
  8. Amy Lee

    Tim Greer Guest

    grocery_stocker wrote:

    > On Oct 14, 2:12 pm, Tim Greer <> 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!
    Tim Greer, Oct 15, 2008
    #8
  9. Amy Lee

    Guest

    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.
    , Oct 15, 2008
    #9
  10. Amy Lee

    Tim Greer Guest

    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!
    Tim Greer, Oct 15, 2008
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rtm
    Replies:
    0
    Views:
    785
  2. Puneet
    Replies:
    16
    Views:
    1,113
    Daniel Vallstrom
    Mar 20, 2005
  3. Replies:
    4
    Views:
    404
  4. Lakshmanan.ag
    Replies:
    1
    Views:
    1,405
    eliascm
    Apr 12, 2010
  5. Yesterday Paid
    Replies:
    1
    Views:
    251
    Dave Angel
    Jun 21, 2012
Loading...

Share This Page