Hi

I am trying to parse a text file so print subsection of a file based on how many times the start and end strings are present in the file.

Now my sample.txt file is


1st line to ignore
2nd line to ignore
print from this line 1
lets print the following line too 1
Okay lets close it now 1
closing at this line 1
this was fine
but now is the problem
2nd line to ignore
print from this line as well 2
lets print the following line too 2
Okay lets close it too now 2
closing at this line 2
No I dont want to print this line
bye

I want my output to be


print from this line 1
lets print the following line too 1
Okay lets close it now 1
closing at this line 1
print from this line as well 2
lets print the following line too 2
Okay lets close it too now 2
closing at this line 2


But not able to achive the same.Tried two piece of codes:-

#!/usr/bin/perl use strict; use warnings; undef $/; open (FILE, '<', 'sample.txt') or die "Could not open sample.txt: +$!"; my $file = <FILE>; my ($printline) = $file =~ m/.*?(print from.*?closing).*/sg; print $printline; close (FILE) or die "Could not close sample.txt: $!";
and
open (FILE, '<', 'sample.txt') or die "Could not open sample.txt: +$!"; open (FILE1, '>', 'output.txt') or die "Could not open output.txt: + $!"; my @file = <FILE>; # Whole file here now... my $sizeconf = scalar @file; #print $sizeconf; my $j = 0; #print $file[2]; for (my $i ; $i < $sizeconf ; $i++) { if ($file[$i] =~ /print from/){ #print $i; print $file[$i] if ($file[$i] !~ /closing/); my $sum = $i+1; for ($sum; $i < $sizeconf ; $i++){ #$sum = $i+1; print "$file[$i+1]"; #$i++ ; if ($file[$i] =~ /closing/ ){ print $file[$i]; break; } } } } close (FILE) or die "Could not close sample.txt: $!"; close (FILE1) or die "Could not close output.txt: $!";
. Kindly help.

In reply to Parsing a file to print multiple times by ExperimentsWithPerl

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.