To parse files..

Discussion in 'Perl Misc' started by clearguy02@yahoo.com, Feb 2, 2005.

  1. Guest

    Hi perl experts:

    I have the following issue.

    I have a list of 5 Windows machines and I have a command
    "C:\uptime.exe" to see if any of these five machines are not alive. If
    a machine is not alive, it gives an output message "UPTIME was unable
    to connect to host: \\Machine". For instance, the three machines,
    JOHN-2K, ANNA5 and Test machines are not alive and you would see the
    first line as ""UPTIME was unable to connect to host: \\JOHN-2K".

    My code:
    ============================
    open (OUT1, ">D:\\test5.txt") || die " Can not write to the file: $!";

    foreach (<DATA>)
    {
    print OUT1 "c:\\uptime $_ ";
    }
    close (OUT1);

    open (IN2, "D:\\test5.txt") || die " Can not open the file: $!";
    @IN = <IN2>;

    open (OUT2, ">D:\\test6.txt") || die " Can not write to the file: $!";

    foreach (@IN)
    {
    next unless /^\s*UPTIME was unable to connect to host/;
    print ???????
    }


    __DATA__
    BOB-2K
    JOHN-2K
    SMITH-2
    ANNA5
    Test
    ============================

    I know that I need to use "system" command instead of the "print"
    command in the script. But I don't know how to run the system command
    and print the output to a file handle at the same time.

    Finally, I need to get only the following lines in output file,
    D:\\test6.txt:

    ======================================
    UPTIME was unable to connect the followng hosts:
    1. JOHN-2K
    2. ANNA2
    3. Test
    ======================================

    How can I modify my script to get this output?
    Thanks in advance,
    Rider.
    , Feb 2, 2005
    #1
    1. Advertising

  2. wrote:

    > print OUT1 "c:\\uptime $_ ";


    > I know that I need to use "system" command instead of the "print"
    > command in the script. But I don't know how to run the system command
    > and print the output to a file handle at the same time.


    Use backticks like this: print OUT1 `c:\\uptime $_ `;

    You can also do it all in one loop.

    --

    Rasto Levrinc
    http://sourceforge.net/projects/rlocate/
    Rasto Levrinc, Feb 2, 2005
    #2
    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. Friso Wiskerke

    How can I let VS.NET 2005 parse .js files?

    Friso Wiskerke, Jan 16, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    416
    Friso Wiskerke
    Jan 16, 2006
  2. Dale
    Replies:
    2
    Views:
    6,841
    1fonzy
    Jun 19, 2010
  3. John
    Replies:
    9
    Views:
    383
    Andrew Thompson
    Jan 14, 2005
  4. Replies:
    19
    Views:
    1,106
    Daniel Vallstrom
    Mar 15, 2005
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    175
    7stud --
    Feb 20, 2008
Loading...

Share This Page