> -----Original Message-----
> From: Najamuddin, Junaid [mailto:[EMAIL PROTECTED]]
> Sent: Friday, August 17, 2001 4:06 PM
> To: [EMAIL PROTECTED]
> Subject: Sort by time the logs for last one hour
> 
> 
> hi,
> I am new to Perl
> I wrote a script which basically pipe delimit the txt file 
> What I want to do is to sort the records along with delimit by time 
> I need the out put to show logs for the last one hour
> Can some one help me on this
> the text file has a column showing start time which shows 
> both date and time
> when the task started
> Here is the code
> 
> # picks up the file out.txt from c drive and after pipe delimit 
> # put the output in outfinal.txt
> 
> my $filename = "c:\\out.txt";
> my $outfile = "c:\\outfinal.txt";
> open (FILE,$filename) || die "Cant open $filename - $!\n";
> open (OUTFILE,">$outfile") || die "Cant open $outfile - $!\n";
> @lines = <FILE>;
> close FILE;
> 
> foreach $lines (@lines)
> {
>   $lines =~ s/[ ,:]\s+/|/g; # delimit pipe
>   print OUTFILE ("$lines\n");
> };
> 
> close OUTFILE;

I'm not really sure what the purpose of the pipe delimiting is (are you
going
to feed this data to Excel or some database?), but based on the format of
your
data file, I don't think your delimiting scheme is going to work. The data
records look like they are in fixed columns, which would call for using
unpack() or substr() to break it into fields. In addition, there are lots of
non-data lines in the file that you need to discard.

It sounds like what you are trying to do is:

   A. Select data rows for the last hour (based on start time?).
   B. Sort those rows by (start?) time.
   C. Output those rows with pipe chars delimiting each field.

Is that correct? If so, here's some general pseudo-code for how I would
approach it:

   while (<>)
   {
       chomp;

       # << determine whether current line in $_ is a data row >>
       next unless $is_data_row;

       # << determine whether data row is within last hour >>
       next unless $within_last_hour;

       push @data, $_;          # save this row
   }

   for (sort bytime @data)      # bytime() needs to order rows by time
   {
       # $template below is an unpack() template defining each field
       @fields = unpack $template, $_;
       s/^\s+//, s/\s+$// for @f;
       print join('|', @f), "\n";
   }

In order to deal with the date/time stamps, you could use the 
Date::Manip module. The date/time format is one of those recognized 
by ParseDate().


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

Reply via email to