Re: Need help with a question.
- From: Trev <trevor.dodds@xxxxxxxxx>
- Date: Sat, 28 Jun 2008 19:07:21 -0700 (PDT)
On Jun 28, 6:22 pm, Ben Morrow <b...@xxxxxxxxxxxx> wrote:
Quoth Trev <trevor.do...@xxxxxxxxx>:
On Jun 28, 1:40 pm, Ben Morrow <b...@xxxxxxxxxxxx> wrote:
Thanks Ben for the detailed reply, I've been able to get the output
correct, but I'm stuck on how to read $cpqlogline into an array
without writing it to a file.
my files look like this:
the perl file:
use warnings;
use strict;
my $server_file = "list.txt";
my $mactmp = "mactmp.txt";
my $maclog = "logfile.txt";
sub LoadFile
{
open(my $SRV, '<', $server_file)
or die("Could not open file: $!");
while (my $server = <$SRV>) {
chomp($server);
open (my $DAT, '<', 'output.txt')
|| die("Could not open 'output.txt': $!");
If you reopen output.txt every time you will start at the beginning
again.
open (OTF, ">blah.txt");
print OTF $DAT;
What are you trying to achieve here? You don't ever seem to make use of
the contents of blah.txt.
while (my $cpqlogline = <$DAT>) {
{
chomp($cpqlogline);
if ($cpqlogline =~ /MAC/)
{
$cpqlogline =~ s/ <FIELD NAME="Subject" VALUE="//i;
$cpqlogline =~ s/ <FIELD NAME="MAC" VALUE="//i;
$cpqlogline =~ s/"\/>//i;
open (TMP, ">>$mactmp");
print TMP "$server". "," . "$cpqlogline\n";
close TMP;
You don't need to keep everything in temporary files like this. Perl has
variables for keeping data in.
Here you write all the data into mactmp.txt; in fact, you write a line
for every server with every mac address. I don't think this is what you
meant.
}
}
}
}
close OTF;
}
sub CreateLOG
{
open (TMP, "<$mactmp");
open (LOG, ">>$maclog");
my @lines=<TMP>;
print LOG "$lines[1]";
Here you reopen mactmp.txt, read all the data into @lines, print the
second line (only) to maclog.txt, and throw the rest away. This is why
you only get one record in the log at the end.
I'm not quite sure how to help you at this point. You seem to be missing
several rather fundamental points about how Perl works. If I were
writing this program, I might write it something like this (completely
untested):
#!/usr/bin/perl
my $srvs = 'list.txt';
my $macs = 'output.txt';
my $log = 'logfile.txt';
# Only open each file once. We don't need any temporary files.
open my $SRVS, '<', $srvs or die "can't read '$srvs': $!";
open my $MACS, '<', $macs or die "can't read '$macs': $!";
open my $LOG, '>>', $log or die "can't append to '$log': $!";
# For each line in output.txt...
while (<$MACS>) {
# if it matches the pattern, put the VALUE field in $mac....
my ($mac) = /<FIELD NAME="MAC" VALUE="([^"]+)">/
# otherwise move on to the next line.
or next;
# Get the next server from the list.
my $srv = <$SRVS> or die "Not enough servers.\n";
chomp $srv;
# Write a line to the logfile.
print $LOG "$srv,$mac\n";
}
# Close the logfile explicitly so we can check all the data got
# written safely.
close $LOG or die "writing to '$log' failed: $!";
# Check if there are any servers left.
<$SRVS> and die "Not enough addresses.\n";
__END__
Can you understand what that's doing?
Ben
--
We do not stop playing because we grow old;
we grow old because we stop playing.
b...@xxxxxxxxxxxx
Hi
I'm only after 1 line (MAC) per server, which is value [1] the other
MAC's I do not need. Is it possible to grep MAC from the Array (Entire
File) and pass the results into a new array which I could then
print[1] to a file. This will then extract 1 line for every server
that I have an xml file for.
.
- Follow-Ups:
- Re: Need help with a question.
- From: Trev
- Re: Need help with a question.
- References:
- Need help with a question.
- From: Trev
- Re: Need help with a question.
- From: Ben Morrow
- Re: Need help with a question.
- From: Trev
- Re: Need help with a question.
- From: Ben Morrow
- Need help with a question.
- Prev by Date: Re: how do prlglobs expand (was Re: 'nobody' using sudo -- scary!)
- Next by Date: Re: [OT] How to make list of all htm file...
- Previous by thread: Re: Need help with a question.
- Next by thread: Re: Need help with a question.
- Index(es):
Relevant Pages
|