xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have a logfile including three diffrent parts.Then I'd like to do like below:
first part (1 to 10000 line) search "ORA" string second part (10001 to + 20000 line) search "Failed" string third part (200001 to the rest line) search "File" string
My question is: How quickly jump to the line I want and begin search?
all reply you can give would be really appreciated!


I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: split file and search respectly
by GrandFather (Saint) on Jan 12, 2007 at 04:12 UTC
Re: split file and search respectly
by prasadbabu (Prior) on Jan 12, 2007 at 04:12 UTC

    Hi xiaoyafeng,

    You have to take a look at Tie::File and perlre.

    From the documentation:

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

    A sample code to proceed....

    use strict; use warnings; use Tie::File; my @file; tie @file, 'Tie::File', "test.txt" or die "$!"; for (0..5) { print "ORA is present\n" if $file[$_] =~ /ORA/; }

    update: added code.

    Prasad

Re: split file and search respectly
by ikegami (Patriarch) on Jan 12, 2007 at 04:18 UTC

    My question is: How quickly jump to the line I want and begin search?

    That's not possible unless you know at which byte offset the desired line starts.

Re: split file and search respectly
by Joost (Canon) on Jan 12, 2007 at 15:04 UTC
    How quickly jump to the line I want and begin search?

    Most operating systems do not store text files as records of lines, i.e. they don't store that information. You will need to read the file from the start keeping a count of the number of lines you've read so far. Luckily perl makes this easy for you as long as you read the file line by line (the default):

    open my $file,"<","/path/to/file.txt" or die "Can't read file: $!"; while (<$file>) { # reads one line of text # the line string is in $_ # the line number is in $. if ($. <= 10000) { print "ORA found on line $.\n" if /ORA/; } elsif ($. <= 20000) { print "Failed found on line $.\n" if /Failed/; } else { print "File found on line $.\n" if /File/; } } close $file;
    See also perlvar and perlopentut and perlretut