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).


In reply to Re^3: Searching for a string in a text and printing the next few lines by ww
in thread Searching for a string in a text and printing the next few lines by Anonymous Monk

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, details, 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, summary, 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.