C
clearguy02
Hi all,
I have the following scenario.
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
output file, D:\test6.txt as
==========================================
UPTIME was unable to connect to host: \\JOHN-2K
UPTIME was unable to connect to host: \\ANNA5
UPTIME was unable to connect to host: \\Test
===========================================
Actual Code:
------------------------------------------------------
open (OUT1, ">D:\\test5.txt") || die " Can not write to the file: $!";
foreach (<DATA>)
{
print OUT1 `c:\\uptime $_\n`;
}
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 OUT2 $_;
}
close (OUT2);
__DATA__
BOB-2K
JOHN-2K
SMITH-2
ANNA5
Test
----------------------------------
I know that I have written a lot of redundant code here. How can I
directly parse the first output (in test5.txt) for the line that starts
with "UPTIME was unable to connect to host" string and get only those
lines in the final output file, test6.txt? I think we can avoid the
test5.txt and directly get the desired output into test6.txt.
Thanks,
Rider.
I have the following scenario.
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
output file, D:\test6.txt as
==========================================
UPTIME was unable to connect to host: \\JOHN-2K
UPTIME was unable to connect to host: \\ANNA5
UPTIME was unable to connect to host: \\Test
===========================================
Actual Code:
------------------------------------------------------
open (OUT1, ">D:\\test5.txt") || die " Can not write to the file: $!";
foreach (<DATA>)
{
print OUT1 `c:\\uptime $_\n`;
}
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 OUT2 $_;
}
close (OUT2);
__DATA__
BOB-2K
JOHN-2K
SMITH-2
ANNA5
Test
----------------------------------
I know that I have written a lot of redundant code here. How can I
directly parse the first output (in test5.txt) for the line that starts
with "UPTIME was unable to connect to host" string and get only those
lines in the final output file, test6.txt? I think we can avoid the
test5.txt and directly get the desired output into test6.txt.
Thanks,
Rider.