To parse files..

C

clearguy02

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.
 
R

Rasto Levrinc

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.
 

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,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top