in reply to Get file part from the section where the matched pattern is found.

#! /usr/bin/perl -w use strict; open(FILE,"< file1.txt"); my @lines=<FILE>; close(FILE); my $found=0; foreach my $line(@lines) { if($found) { print "$line"; } if($line=~/start/) { $found=1; print "$&$'"; } }
output
startdexdbnjcrk this when be shown this will be shown this will also be shown
contents of file were
anjwdwus svwbsdncelf,c;rf wscgwhjdke ff ed iekd ewd asvdjcn startdexdbnjcrk this when be shown this will be shown this will also be shown

Replies are listed 'Best First'.
Re^2: Get file part from the section where the matched pattern is found.
by rockstar99 (Novice) on Nov 29, 2011 at 10:57 UTC
    Hi Ansh, Thanks for that code. How could I modify the code if I want to print
    startdexdbnjcrk this when be shown
    from the sample text you have chosen. Thanks in advance.

      if you meant that you want to print the line with the regex and the next line to it
      then simply add last; in the first if section

      if($found) { print "$line"; last; }
      this will get you the output
      startdexdbnjcrk this when be shown

      UPDATE:and if you want to print more lines then youll be needing a counter and check each time with "if" weather the desired number of lines have been printed or not. if yes then exit.


      there be many shortcuts but i guess this one is the simplest for a newbie like me to understand