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

I have a requirement where I have to scan a file, search for a string and print the next 10 lines of the file starting from the line where the string was found.

E.g.

MyFile

This is line1
This is line2
Did you Findme
This is line3
This is line4
.
.
.

String to find "Findme"

Output should be

Did you Findme
This is line3
This is line4
.
.
.

Also, the output should be written to another file

  • Comment on Searching for a string in a text and printing the next few lines

Replies are listed 'Best First'.
Re: Searching for a string in a text and printing the next few lines
by Corion (Patriarch) on Nov 12, 2009 at 13:34 UTC

    So, where is your problem?

    This site is about learning and using Perl. It is is not a script writing site where you post your specifications and get fully functional scripts in return. So please, post the code you have written, and tell us where exactly you encounter problems and what you've tried so far to solve them and how it failed for you.

      This is one of the reasons why I love this site. It very much seemed to me like a homework/interview question.
      Before you go asking for help, at least try it yourself!
      /\ Sierpinski
Re: Searching for a string in a text and printing the next few lines
by Utilitarian (Vicar) on Nov 12, 2009 at 13:45 UTC
    What Corion said, that's not a question it's a spec.
    • use strict;
    • use warnings;
    • open files
    • check each line of source file
      • If it matches set a found_it flag to 10
      • if found_it is set
        • print line to output file
        • decrement found_it
    • That's it
    Have a go at coding up the above in Perl.
    When it doesn't work and the error messages don't point you in the right direction
          use diagnostics;
    If that fails ask a question with some evidence of effort on your part.
Re: Searching for a string in a text and printing the next few lines
by ww (Archbishop) on Nov 12, 2009 at 23:33 UTC

    Can your search term occur more than once in the file? If so, what do you want to happen when a second instance is fewer than ten lines after the first? Is it possible that a "Findme" occurs on the last line of the file and if so, what do you want to happen then? For example:

    This is line1 At line 2. Did you Findme This is line3 This is line4 this is another Findme embedded in line 5 This is line 6 this is line 7 this is line 8 Line 9: yet another findme line 10 line 11 line 12 line 13 line 14 line 15 line 16 line 17 line 18 line 19 is the 10th line after the next-to-last instance of searchterm line 20 line 21 and so on... line 23 24: last Findme

    IMO, you'll need no more than beginner skills to write a script (solution OR attempt-at-a-solution STILL left to the OP) that produces this output:

    At line 2. Did you Findme This is line3 This is line4 this is another Findme embedded in line 5 This is line 6 this is line 7 this is line 8 Line 9: yet another findme line 10 line 11 line 12 line 13 line 14 line 15 line 16 line 17 line 18 line 19 is the 10th line after the next-to-last instance of searchterm 24: last Findme done

    ...but whether or not this is comensurate with your "requirement" (job? homework? other?) depends on the answers to the questions posed above.

    • If so, a hint: after you deal with the I/O, think in terms of regexen.
      guys guys guys....
      spare me a thought :( I am just a naive finding my way into perl
      I tried working something out but was nowhere, hence had to type the requirement
      anyways, this is the code i tried..

      #! /usr/local/bin/perl open (TEXT_FILE, "file1.txt"); while ($line = <TEXT_FILE>) { if ($line =~ m/ERROR/) { $c = 0; while ($c <= 10) { $nextline = <TEXT_FILE> ; printf ("$nextline"); $c = $c + 1; } } } close (TEXT_FILE);

      and like you said vroom, if the search string is in multiple locations, I want to print all such instances, not just once..

        That's a good start.

        On quick inspection:

        1. Your script won't print the first instance of "ERROR" so you may wish to add a print $line at line 7.
        2. With that change, your script will print the first instance of "ERROR" but won't print the ten lines following a second instance, if that second instance is fewer than ten lines below the first. Is that your intent?
        3. Is there a reason for using printf rather than a simple print?

        You could use @array = open (TEXT_FILE, "file1.txt"); to read your file into an array and then test each element of the array rather than using while. That might make it easier to work out the logic needed to satisfy the "ten lines" requirement.

        AND, read perldoc -f open for the three argument form of open (and always test the open: or die "Can't open file1.txt: $!\n";).

        Life will also be easier if you use clear and consistent indenting (which of the various recommendations you follow is a matter of taste). Hint for posting here: "Preview, perview, preview! A string of spaces renders differently than a tab.

        You'll get better answers if you tell in detail what "nowhere" means, in terms of output, error & warnings messages and a better set of sample data (see my previous questions: you've answered some of them, but left some crucial elements of your spec to our crystal balls - most of which are still broken).