Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > To parse files..

Reply
Thread Tools

To parse files..

 
 
clearguy02@yahoo.com
Guest
Posts: n/a
 
      02-02-2005
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.

 
Reply With Quote
 
 
 
 
Rasto Levrinc
Guest
Posts: n/a
 
      02-02-2005
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
optparse: parse v. parse! ?? 7stud -- Ruby 3 02-20-2008 05:20 AM
How to parse a string like C program parse the command line string? linzhenhua1205@163.com C Programming 19 03-15-2005 07:41 PM
Parse Text File and Output to File John M. Lembo Perl 0 08-01-2003 04:34 PM
Parse an xml file with line breaks in the beginning Raj Mudaliar Perl 0 07-14-2003 06:00 PM
[TABLE NOT SHOWN] problem with HTML::Parse Mitchua Perl 3 07-13-2003 11:38 PM



Advertisments