How to read the last line of a large file in Perl under Windows (Summary)

Source: Internet
Author: User



There are many ways to read the last line of a file in Perl, such as



(1) Read the file into the array and take the last element

open (FILE, "file.txt") or die "$!";
my @ arr = <FILE> ;;
close FILE;
my $ last = $ arr [$ # arr];

# $ last is the content of the last line.
(2) Read in line by line, output when the last line

open (FILE, "file.txt") or die "$!";
while (<FILE>;)
{
    open (TMP, "> tmp.txt") or die "$!";
    print TMP $ _;
    close TMP;
}
close FILE;
or

open (FILE, "file.txt");
$ a = <FILE> ;; push (@ a, $ a);
while (<FILE>)
{
     push (@a, $ _);
    [email protected];
}
close FILE;
print @a;
(3) This has not been tried, you can try if you are interested

http://search.cpan.org/~mjd/Tie-File-0.96/lib/Tie/File.pm
DESCRIPTION ^

Tie :: File represents a regular text file as a Perl array. Each element in the array corresponds to a record in the file. The first line of the file is element 0 of the array; the second line is element 1, and so on .

The file is not loaded into memory, so this will work even for gigantic files.

Changes to the array are reflected in the file immediately.

Lazy people and beginners may now stop reading the manual.

(4) The above methods all need to traverse the entire file, which is inefficient. The following method is the most efficient, especially when the file is large, the method is:

Open the file, move the pointer to the end, read one byte at a time until it reaches \ N

#! / usr / bin / perl -w

use strict;

my $ file = shift or die;
my $ content = "";

open (F, $ file) or die $ !;
seek (F, 0, 2); # set handler at the end of $ file
until ($ content = ~ m / \ n (. *) \ n? $ /)
{
        my $ string;
        if (seek (F, -1024, 1)) # backward 1024 bytes
        {
                my $ n = read (F, $ string, 1024) or die $ !;
                $ content = $ string. $ content;
                last if ($ n <1024);
                seek (F, -1024, 1);
        }
        else {
                my $ len = tell F;
                seek (F, 0, 0) || die "see error at $ file";
                read (F, $ string, $ len) or die $ !;
                $ content = $ string. $ content;
                last;
        }
}
close (F);

if ($ content = ~ m / \ n \ n $ /)
{
        print "\ n";
} elsif ($ content = ~ m / \ n? (. *) \ n? $ /) {
        print "$ 1 \ n";
} else {
        print $ content, "\ n";
}
The content in the article comes from: Question http://bbs.chinaunix.net/thread-599099-1-1.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.