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

Read only the last line of a file??

Expand Messages
  • Chirhart, Brian
    Is it possible to only read in the last line of a file - a log file for example? I want to parse the LOG s that a program makes, but they can get pretty large
    Message 1 of 2 , Apr 1, 2002
    • 0 Attachment
      Is it possible to only read in the last line of a file - a log file for
      example? I want to parse the LOG's that a program makes, but they can get
      pretty large and there are a LOT of them. I only need the information on
      the last line. Any clues?

      The only way I know how to open a file is with:

      open (LOG,$_) ||
      die "Sorry - Died: $!";

      #Code here....
      #

      close(CFG);

      But that opens and examines the entire LOG file. Any help would be
      appreciated.
    • b_harnish
      ... In the thread (viewable at: http://groups.yahoo.com/group/perl-beginner/message/8926): Another user asked a similar question, and Jahpy suggested using the
      Message 2 of 2 , Apr 1, 2002
      • 0 Attachment
        --- In perl-beginner@y..., "Chirhart, Brian" <bchirhart@f...> wrote:
        > Is it possible to only read in the last line of a file - a log file for
        > example? I want to parse the LOG's that a program makes, but they can get
        > pretty large and there are a LOT of them. I only need the information on
        > the last line. Any clues?
        >
        > The only way I know how to open a file is with:
        >
        > open (LOG,$_) ||
        > die "Sorry - Died: $!";
        >
        > #Code here....
        > #
        >
        > close(CFG);
        >
        > But that opens and examines the entire LOG file. Any help would be
        > appreciated.

        In the thread (viewable at: http://groups.yahoo.com/group/perl-beginner/message/8926):
        Another user asked a similar question, and Jahpy suggested using the File::ReadBackwards module for a pre-written implementation of reading the file backwards.

        Using that module, you just read one line (which will be the one at the end of the file), and process that.

        BTW: Your example above opens "LOG" (which is never closed), and closes "CFG" (which is never opened).

        - Brian
      Your message has been successfully submitted and would be delivered to recipients shortly.