I have the following three lines in a text file

i.e text file contains

bit rs_1 ; bit rs_2 ; bit rs_3 ;
when i=2 then the text file should contain
bit rs_1 ; bit rs_2 ;
when i=4 the text file should contain
bit rs_1 ; bit rs_2 ; bit rs_3; bit rs_4;
I have the following code , where is the problem ?
my $file = 'abc.txt'; { open my $in_fh, '<', $file or die "Cannot open input file: $!\n"; flock $in_fh, LOCK_EX | LOCK_NB or die "Cannot obtain a lock on $f +ile: $!\n"; my $temp_out = File::Temp->new(TEMPLATE => "$0-$$-XXXXX"); while (my $line = <$in_fh>) { while ( $line =~ /-abc\s(\d+)/g ) { my $first = $1; if (my ($tail) = $line =~ /^\.march_endSP)(.*)/) { print(".rs_$_$tail\n") for 1..$first; } else { print($line); } } close $in_fh or die "Failed to close $in_fh. Aborting. $!\n"; $temp_out->flush; eval { move($temp_out->filename, $in_fh); } or do { warn "Failed to swap $temp_out into $in_fh: $!\n"; }; }
I am unable to get the output, where is the mistake ?

In reply to How the lines will get modified as per value of i by suvendra123

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.