Tim Martin wrote:
> 
> I have run into a small problem with the script we have been working on for
> log management.
> Problem 1 - The script must be in the same sub directory as all the logs.

What makes you think that it must?

>             How can I get the script to run from /var/tmp/gatelog and not in
> var/tmp/gatelog/glogs

Use perl's chdir function to set the current working directory to
whatever you want.

> Problem 2 - The Line of the log dose not have the correct server name in
> column one.
>             What I'm I doing wrong.
> 
> s00237 2002/12/31 20:47:34 +05 003503E0: STATUS DATA: jobqq= 0 jobqr= 0
> reqdq= 0 gwmethods= 0
> s00237 2002/12/31 21:47:37 +05 003503E0: STATUS DATA: jobqq= 0 jobqr= 0
> reqdq= 0 gwmethods= 0
> 2002/12/31 22:47:45 +05 003503E0: STATUS DATA: jobqq= 0 jobqr= 0 reqdq= 0
> gwmethods= 0
> 
> Below is the script we are using.
> 
> #!/usr/bin/perl -w
> 
> #chdir ("/var/tmp/gatelog/glogs") && die "cannot find directory";
                                    ^^
Using && (and) means that you want it to die right after chdir works
correctly.  You want to use the logical or so that it dies only if chdir
fails.

chdir '/var/tmp/gatelog' or die "Cannot chdir to '/var/tmp/gatelog' $!";


> my @Gatelogs_list = `ls`;

There is no reason to run an external program to get the list of files
in the current directory.

my @Gatelogs_list = glob '*';

In fact, you can get a list of the files in '/var/tmp/gatelog' without
actually being in that directory.

my @Gatelogs_list = glob '/var/tmp/gatelog/*';


> my $lineprint = '';
> my $lines=`ls | wc -l`;

Since you already have the list of files in @Gatelogs_list you can use
that directly.

my $lines = @Gatelogs_list;


> my $i=0;
> my $line = '';
> 
> open (OUT, "> /var/tmp/gatelog/gatelog.txt");

You should _always_ verify that the file was opened.

open OUT, '> /var/tmp/gatelog/gatelog.txt' or die "Cannot open
'/var/tmp/gatelog/gatelog.txt' $!";


> while ($lines >$i){
>     open (IN,"$Gatelogs_list[$i]");

The usual way to do this is with a foreach loop:

foreach my $file ( @Gatelogs_list ) {
    open IN, $file or die "Cannot open '$file' $!";


>     while (<IN>) {
>         chomp; # get rid of the newline
>         next if $_ eq ''; # skip empty lines

A more robust method is to test for zero or more whitespace characters:

          next if /^\s*$/;


>         s/\s{2,}/ /g; # replace 2 or more whitespace chars
>                       # with a single space
>         if ( m{^\d{4}/\d{2}/\d{2} \d\d:\d\d:\d\d} ) {
>             # if the current line starts with a timestamp ...
>             # I assume the YYYY/MM/DD HH:MI:SS format
>             chomp $Gatelogs_list[$i];

There is no point in chomp()ing the file name here as you wouldn't have
been able to open it if it had an extraneous newline at the end.


>             $lineprint = "$Gatelogs_list[$i]" . " " . "$line";

Putting quotes around single scalars is not required, either:

             $lineprint = $Gatelogs_list[$i] . ' ' . $line;

Or:

             $lineprint = "$Gatelogs_list[$i] $line";


>             print OUT $lineprint, "\n" if $line;
>             # print the buffer
>             $line = $_;
>             # remember the current line
>         } else {
>             $line .= '' . $_;

              $line .= $_;


>             # add the current line to the buffer
>             }
>         }
>     close IN;
>     $i++;
>     }
> print OUT $line, "\n" if $line;
> # print the last buffer
> close OUT;
> 
> exit 0;


You could simplify it a bit by storing the filenames to read in @ARGV

#!/usr/bin/perl -w
use strict;

open OUT, '> /var/tmp/gatelog/gatelog.txt' or die "Cannot open
'/var/tmp/gatelog/gatelog.txt' $!";

my @ARGV = glob '/var/tmp/gatelog/*';
my $line = '';
while ( <> ) {
    chomp; # get rid of the newline
    next if /^\s*$/;
    s/\s{2,}/ /g; # replace 2 or more whitespace chars
                  # with a single space
    if ( m{^\d{4}/\d{2}/\d{2} \d\d:\d\d:\d\d} ) {
        # if the current line starts with a timestamp ...
        # I assume the YYYY/MM/DD HH:MI:SS format
        my $lineprint = "$Gatelogs_list[$i] $line";
        print OUT "$lineprint\n" if $line;
        # print the buffer
        $line = $_;
        # remember the current line
        }
    else {
        $line .= $_;
        # add the current line to the buffer
        }
    }
print OUT "$line\n" if $line;
# print the last buffer
close OUT;

exit 0;



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to