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

Hi Monks

I am little bit familiar with perl, I have to make script that opens a file searches for a particular pattern's(Oct-18-2013) first and last occurrence and then dumping the text in between to a new file. Please help.

PS : The input file will be of type html containing a single huge line of 1000s of words

Thanks in advance

Ankur

  • Comment on Searching a pattern's first and last occurrence in a file.

Replies are listed 'Best First'.
Re: Searching a pattern's first and last occurrence in a file.
by BrowserUk (Patriarch) on Oct 18, 2013 at 15:00 UTC
    a single huge line of 1000s of words

    Slurp the file and use index, rindex and substr:

    my $content = do{ local @ARGV = 'theFile.htm'; <> }; my $first = index $content, 'Oct-18-2013'; my $last = rindex $content, 'Oct-18-2013'; print substr $content, $first, $last;;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thanks a lot i am almost done now. Just wanted to ask is there any way to search a file for a string bottom up? I know there is a module file::readbackwards,But i am not able to install it. any other way?

      Secondly how do i search ABCXXX where xxx could be Jan Feb...... Dec

      Thanks

      Ankur

        is there any way to search a file for a string bottom up?

        There are ways, but unless your file(s) are so big that you cannot fit them in memory(?), I wouldn't bother because they are horribly inefficient.

        Secondly how do i search ABCXXX where xxx could be Jan Feb...... Dec

        Use regex.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Searching a pattern's first and last occurrence in a file.
by LanX (Saint) on Oct 18, 2013 at 14:59 UTC
    What did you try?

    Tip Use a regex!

    Cheers Rolf

    ( addicted to the Perl Programming Language)