Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Yes There is surely a way to it...like this...First find the size of files in bytes which is very easy as you know.. After that you just read the last 100bytes of the file(this number will be best depending on how long are your lines in the file) This can be easily done as follows..Suppose your file has 10000 bytes now reading the last 100 bytes is just pointing your file pointer on the 9900 th byte...and grab the 100 bytes then

#!usr/local/bin/perl open(FILE ,"name_of_the_file_to_be_read"); seek(FILE, 9900,0); #point the pointer to 9900th byte from the s +tarting( $ read(FILE,$ab,100); #note the last 100 bytes get stored in $ab close(FILE);
Then as you know search for the newline charcter in variable ab(or as the case may be fixing the input record seperator as reqd) keep in mind that there will be a newline character in the end too.. after finding the new line character you can easily know at which position it is in these 100 bytes.. (say it is on 15th byte in this 100 byte u read) then you can easily retrieve the desired 16th to 100 th byte by using substr() function.. thus you have read the last line.. if you want to read the last but second line from the end. you may please read in similar fashion from 9815bytes to 9915 bytes i.e. just fixing ur pointer at 9815 form the start) provided you have the results as I have told. and proceed further.. for any line number from the end... In case you r not able to get the new line character in the last 100bytes(i.e. the last line is more than 100 bytes long read the last 200 bytes or so and get the last line similarly... The advantage is that it saves you a lot of memory space as you are reading the minimum bytes and also speed if files are too long and one is intrested in contents in the last portion of the file(say last 5%)

Also There is a module to read the file back-wards.. as I have been told by autark...I am quoting his reply...

Indeed, there is a module called File::ReadBackwards available through CPAN. From the description in the man file: This module reads a file backwards line by line. It is simple to use, memory efficient and fast. It supports both an object and a tied handle interface. It is intended for processing log and other similar text files which typically have their newest entries appended to them. By default files are assumed to be plain text and have a line ending appropriate to the OS. But you can set the input record separator string on a per file basis. Hope it helps....Please comment if this helps you...

And note the metod above can be really optimised depending upon your need I have just stated the algorithm..The no of bytes etc.. all depends upon your local requirements..also suppose you want to read the 6th line from the last you don't have to nessearily read the last line & then second last & so on but you just read the last 500 bytes and find how many new line characters are there (you require 5 or 6 depending that there is new line character in the end of last line) when you do have this then read from the first new-line character in variable ab to the nest new line character i.e. you can really optimise it...That's all.. :-vnpandey

Edit: g0n - formatting


In reply to Re: Is there a way to read a file backwards? by vnpandey
in thread Is there a way to read a file backwards? by gaggio

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (9)
    As of 2018-07-16 09:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















      Results (333 votes). Check out past polls.

      Notices?