If you change from the scalar context of grep to the list context, it'll capture the lines that match and then you can print out the list of matching lines.

The codes would look something like this:

open(FILE,"<",$filepath) or die "Couldn't open file for writing: $!"; @error1 = grep { /0 Items in Feed & 0 New Fetched Items/ } <FILE>; if(scalar(@error1)>0) { print "match found\n"; print "@error1"; } else { print "nothing"; } close FILE;

I tried the above snippet on a test file and it works like I believe you are hoping.

Note several things. First, I find that it is generally a good idea to use the 3-argument form of the open as I did in the above.

Second, since the code is storing the lines returned by grep into a list, it is easy to see if any lines matched simply by checking the number of entries in the list (i.e., by using if(scalar(@error)>0){...more code...}. It is, of course equally valid to use if(@error1 > 0){...more code...} since the if() forces @error1 into scalar context so that it directly returns the number of elements in it. I prefer (just my own preference) to explicitly use the scalar() function to remind me that I'm looking for the number of elements in the list.

Third, I do print "@error1" under the assumption (as reflected in my code snippet and in yours) that the lines already contain a "new line" character (i.e., "\n") at the end of each line). I make that assumption because there is no chomp() after reading in the lines (you could add chomp; into the block code in your grep before the reqex match). Given that they already contain the new line, then just doing a <c>print "@error1" will result in output that is already line separated.

Finally, I much prefer the other responders' suggestions to use while() rather than grep() so that you only read one line at a time into your space. grep(), as others wisely note, first slurps the entire file into your working space as a list and then operates on that list. Using the while() construct only works with one line at a time which is much more conservative of work space. Of course, if your FILE is not very big then the grep() may be a little more succinct and compact. But, from my perspective, that's for you to decide.

I hope this helps.

ack Albuquerque, NM

In reply to Re: printing the line after grepping by ack
in thread printing the line after grepping by rajyalakshmi

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.