Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] viewing log files

Expand Messages
  • tomroth@wfubmc.edu
    ... Here s what finally worked! Thanks to all who made suggestions. The most recent log entry is still appended to the bottom of the log file but when the
    Message 1 of 6 , Feb 2, 2001
    • 0 Attachment
      > The most recent log entry goes to the bottom of the logfile.
      > What I'd really like is for it to go to the top.

      Here's what finally worked! Thanks to all who made suggestions.

      The most recent log entry is still appended to the bottom of the log
      file but when the view_log.cgi reads it in it is reversed it so that it
      is displayed with the most recent at the top.


      open (FH, "$logfile") || &error("Unable to open auth.log.");
      @log = <FH>;
      close FH;
      @log_rev = reverse @log;
      foreach $line(@log_rev){
      print "$line";
      }


      However, I didn't get any takers on the second question.

      How can I remove just the first 10 lines of a text file?


      --
      Tom Roth • tomroth@... • tel 336.716.4493
      Wake Forest University School of Medicine
      Dept of Biomedical Communications
      Medical Center Blvd • Winston-Salem, NC 27157-1011
      http://www.wfubmc.edu/biomed/
    • Mike Payne
      Untested, but this should work. open(FILE, file.txt ); @file = ; close(FILE); for (1..10) { shift(@file); } open(FILE, file.txt ); foreach $_ (@file) {
      Message 2 of 6 , Feb 2, 2001
      • 0 Attachment
        Untested, but this should work.

        open(FILE,"file.txt");
        @file = <FILE>;
        close(FILE);

        for (1..10) { shift(@file); }

        open(FILE,">file.txt");
        foreach $_ (@file) { print FILE $_; }
        close(FILE);

        -----Original Message-----
        From: tomroth@... [mailto:tomroth@...]
        Sent: Friday, February 02, 2001 2:35 PM
        To: perl-beginner@yahoogroups.com
        Subject: Re: [PBML] viewing log files



        > The most recent log entry goes to the bottom of the logfile.
        > What I'd really like is for it to go to the top.

        Here's what finally worked! Thanks to all who made suggestions.

        The most recent log entry is still appended to the bottom of the log
        file but when the view_log.cgi reads it in it is reversed it so that it
        is displayed with the most recent at the top.


        open (FH, "$logfile") || &error("Unable to open auth.log.");
        @log = <FH>;
        close FH;
        @log_rev = reverse @log;
        foreach $line(@log_rev){
        print "$line";
        }


        However, I didn't get any takers on the second question.

        How can I remove just the first 10 lines of a text file?


        --
        Tom Roth • tomroth@... • tel 336.716.4493
        Wake Forest University School of Medicine
        Dept of Biomedical Communications
        Medical Center Blvd • Winston-Salem, NC 27157-1011
        http://www.wfubmc.edu/biomed/
      • daymobrew@yahoo.com
        ... if ( open (FH, $logfile ) ) { my $LinesRead = 0; my @log while ( ( ) && ( $LinesRead
        Message 3 of 6 , Feb 2, 2001
        • 0 Attachment
          > How can I remove just the first 10 lines of a text file?


          if ( open (FH, "$logfile") )
          {
          my $LinesRead = 0;
          my @log
          while ( ( <FH> ) && ( $LinesRead < 10 ) )
          {
          chomp;
          push @log, $_;
          $LinesRead++;
          }
          close FH;
          }

          This has not been tried - running out to lunch. The bounds may not be
          perfect but I think the algorithm is sound(ish).

          Damien.

          --- In perl-beginner@y..., tomroth@w... wrote:
          >
          > > The most recent log entry goes to the bottom of the logfile.
          > > What I'd really like is for it to go to the top.
          >
          > Here's what finally worked! Thanks to all who made suggestions.
          >
          > The most recent log entry is still appended to the bottom of the log
          > file but when the view_log.cgi reads it in it is reversed it so that
          it
          > is displayed with the most recent at the top.
          >
          >
          > open (FH, "$logfile") || &error("Unable to open auth.log.");
          > @log = <FH>;
          > close FH;
          > @log_rev = reverse @log;
          > foreach $line(@log_rev){
          > print "$line";
          > }
          >
          >
          > However, I didn't get any takers on the second question.
          >
          > How can I remove just the first 10 lines of a text file?
          >
          >
          > --
          > Tom Roth • tomroth@w... • tel 336.716.4493
          > Wake Forest University School of Medicine
          > Dept of Biomedical Communications
          > Medical Center Blvd • Winston-Salem, NC 27157-1011
          > http://www.wfubmc.edu/biomed/
        • Charles K. Clarkson
          ... From: ... After close FH; you could simply: print reverse @log; and get the same result. ... @log = @log[10 .. $#log]; should delete
          Message 4 of 6 , Feb 2, 2001
          • 0 Attachment
            ----- Original Message -----
            From: <tomroth@...>
            >
            > > The most recent log entry goes to the bottom of the logfile.
            > > What I'd really like is for it to go to the top.
            >
            > Here's what finally worked! Thanks to all who made suggestions.
            >
            > The most recent log entry is still appended to the bottom of the log
            > file but when the view_log.cgi reads it in it is reversed it so that it
            > is displayed with the most recent at the top.
            >
            >
            > open (FH, "$logfile") || &error("Unable to open auth.log.");
            > @log = <FH>;
            > close FH;
            > @log_rev = reverse @log;
            > foreach $line(@log_rev){
            > print "$line";
            > }

            After 'close FH;' you could simply:

            print reverse @log;

            and get the same result.

            >
            >
            > However, I didn't get any takers on the second question.
            >
            > How can I remove just the first 10 lines of a text file?
            >

            @log = @log[10 .. $#log];

            should delete the first ten lines, while:

            @log = @log[0 .. @log - 10];

            should delete the last ten lines.

            To remove the first ten lines from a file, read the file
            into an array as you did above. Close the file and then
            open it again and write the altered array to the file:

            open (FH, $logfile) || &error("Unable to open auth.log.");
            @log = <FH>;
            close FH;
            # Opening a file with the '>' prefix will clobber
            # or empty it.
            open (FH, ">$logfile") || &error("Unable to open auth.log.");
            print FH @log[10 .. $#log];
            close FH;

            How it works:
            @log[0 .. 9] represents the first 10 items of the array @log
            and is called an array slice. The '..' is an operator that creates
            list in increments of 1. So [0 .. 9] is the same as
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Note that the righthand integer must
            be less than the lefthand integer. [-5 .. -2] yields [-5, -4, -3, -2]
            If @log had 20 lines in it and we wanted to remove lines
            0 through 9, we would start our array slice at 10.
            @log[10 .. 19] would be an array slice of the last 10 lines .
            Note that 19 is the last index of the array which is also defined
            as $#log in perl. So @log[$n .. $#log] is an array slice of
            @log that includes all but the first $n lines.
            (Assuming $n < $#log.)

            HTH,
            Charles K. Clarkson
          • Vaughn Reavis
            Charles, Thanks for your expanded explanation. You are helping more people then just the questioner and I appreciate that. Vaughn Reavis From: Charles K.
            Message 5 of 6 , Feb 3, 2001
            • 0 Attachment
              Charles,

              Thanks for your expanded explanation. You are helping more people then just
              the questioner and I appreciate that.

              Vaughn Reavis

              From: "Charles K. Clarkson" <c_clarkson@...>
              Reply-To: perl-beginner@yahoogroups.com
              To: <perl-beginner@yahoogroups.com>
              Subject: Re: [PBML] viewing log files
              Date: Fri, 2 Feb 2001 15:27:28 -0600


              ----- Original Message -----
              From: <tomroth@...>
              >
              > > The most recent log entry goes to the bottom of the logfile.
              > > What I'd really like is for it to go to the top.
              >
              > Here's what finally worked! Thanks to all who made suggestions.
              >
              > The most recent log entry is still appended to the bottom of the log
              > file but when the view_log.cgi reads it in it is reversed it so that it
              > is displayed with the most recent at the top.
              >
              >
              > open (FH, "$logfile") || &error("Unable to open auth.log.");
              > @log = <FH>;
              > close FH;
              > @log_rev = reverse @log;
              > foreach $line(@log_rev){
              > print "$line";
              > }

              After 'close FH;' you could simply:

              print reverse @log;

              and get the same result.

              >
              >
              > However, I didn't get any takers on the second question.
              >
              > How can I remove just the first 10 lines of a text file?
              >

              @log = @log[10 .. $#log];

              should delete the first ten lines, while:

              @log = @log[0 .. @log - 10];

              should delete the last ten lines.

              To remove the first ten lines from a file, read the file
              into an array as you did above. Close the file and then
              open it again and write the altered array to the file:

              open (FH, $logfile) || &error("Unable to open auth.log.");
              @log = <FH>;
              close FH;
              # Opening a file with the '>' prefix will clobber
              # or empty it.
              open (FH, ">$logfile") || &error("Unable to open auth.log.");
              print FH @log[10 .. $#log];
              close FH;

              How it works:
              @log[0 .. 9] represents the first 10 items of the array @log
              and is called an array slice. The '..' is an operator that creates
              list in increments of 1. So [0 .. 9] is the same as
              [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Note that the righthand integer must
              be less than the lefthand integer. [-5 .. -2] yields [-5, -4, -3, -2]
              If @log had 20 lines in it and we wanted to remove lines
              0 through 9, we would start our array slice at 10.
              @log[10 .. 19] would be an array slice of the last 10 lines .
              Note that 19 is the last index of the array which is also defined
              as $#log in perl. So @log[$n .. $#log] is an array slice of
              @log that includes all but the first $n lines.
              (Assuming $n < $#log.)

              HTH,
              Charles K. Clarkson







              _________________________________________________________________
              Get your FREE download of MSN Explorer at http://explorer.msn.com
            Your message has been successfully submitted and would be delivered to recipients shortly.