Parsing files

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 w­as unable

to connect to host: \\Machine". For instance, the three mach­ines,
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.
 
T

Tore Aursand

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.

Let me see if I understand you correctly: You want to write the output
of 'uptime.exe' to a file, but only for those computers that can be
connected to?

If that is so, I think you should be able to get away with this
_untested_ script:

#!/usr/bin/perl
#
use strict;
use warnings;

my $output = 'd:/test6.txt';
my $trigger = '^\s*UPTIME was unable to connect';
my $program = 'c:/uptime';

open( OUT, '>', 'd:/test6.txt' ) or die "$!\n";
foreach ( <DATA> ) {
print $_ unless ( `$program $_` =~ /$trigger/ );
}
close( OUT );

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

I have never tested putting variables inside ``, and I have never tested
checking the output from it directly, either. :)
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top