Pulling Data From Log Files and Create a New Log File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CTRC
    New Member
    • Feb 2007
    • 1

    Pulling Data From Log Files and Create a New Log File

    Hello, I'm running into a wall when trying to figure out the Perl code to complete the following.

    I need run a perl script that opens up a server log file (lets call this file SERVER_LOG.txt, located at c:\logs) and then copy the last 11 lines of that file and then print those 11 lines into a new log file (lets call it FINAL_LOG.txt)

    I can open a new log file and write to it, but my problem is opening the current log file and copying the last 11 lines. I would greatly appreciate any code that might help me complete this!! Thank you for your time and help!
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Greetings.

    Show us the code that you have so far.

    Comment

    • IvaPopova
      New Member
      • Jan 2007
      • 4

      #3
      I'm not experienced in this, but maybe you could reverse the file, and then reed the first 11 lines.

      Something like this:
      print REV_FILE reverse <FILE>;

      Comment

      • docsnyder
        New Member
        • Dec 2006
        • 88

        #4
        @CTRC

        For log files which fit into memory:
        Code:
        open(FD, "<", $yourLogFile) or die "ERROR: $!";
        
        @lines = <FD>;
        
        close(FD);
        
        @lastTwelveLines = splice(@lines, scalar(@file)-12, 12);
        
        print @lastTwelveLines;
        For huge log files, which would presumably not fit into memory:
        Code:
        open(FD, "<", $yourLogFile) or die "ERROR: $!";
        
        while ( $line = <FD> ) {
          push(@lastTwelveLines, $line);
        
          @lastTwelveLines = splice(@lastTwelveLines, 1, 12) if ( scalar(@lastTwelveLines) > 12 );
        }
        
        close(FD);
        
        print @lastTwelveLines;
        @miller

        For what do you need to see code in order to answer the question of CTRC??? I prefer to give a simple answer to a simple question ;o)

        Greetz, Doc

        Comment

        • jkr
          New Member
          • Feb 2007
          • 3

          #5
          Depending on what unix you are using maybe the system call "tail -n 11" will do what you want.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            there are also modules for efficiently reading files backwards. You could also get the number of lines in the file and use the $. variable to get only the last 11. But if the file is changing constantly that might not work. You can also use Tie::File and simply read the tied array in reverse.

            Comment

            • miller
              Recognized Expert Top Contributor
              • Oct 2006
              • 1086

              #7
              Originally posted by docsnyder
              Code:
              @lastTwelveLines = splice(@lines, scalar(@file)-12, 12);
              Small syntax error in that line of code as @file does not exist. Fix and simplify to:

              Code:
              @lastTwelveLines = splice(@lines, -12);
              Originally posted by docsnyder
              @miller

              For what do you need to see code in order to answer the question of CTRC??? I prefer to give a simple answer to a simple question ;o)

              Greetz, Doc
              Yes, his question was easy. However, he said that he had already created code and gotten stuck, and as a first time user I wanted to encourage him to share what he already had in order for us to be able to more directly diagnose where the block in his logic or experience was.

              Nevertheless, you've helped him out regardless. Thanks for sharing your expertice.

              Comment

              Working...