A completely perlish way of saying what you want:
#!/usr/bin/perl -w @holdspace = (); while (<>) { if (/'(\d)'/) { s/^/(+$1) /g; s/$/;fac=$1/g; unshift @holdspace, $_; } else { if (@holdspace) { print "Newline1\n", @holdspace; @holdspace=(); } print; } if (/Line5/) { print "MeanLine\n"; } }
Note that this does not depend on the specific digits "1", "2", or "3" - instead, it merely depends on lines containing a digit in single quotes, and that the lines to be reversed are sequential. (And that there's at least one line after the lines with single-quoted digits, though that's an easy restriction to lift) If you'd rather have something that more closely mirrored what the sed script was, (I'm guessing here, since I haven't seen the whole sed script) you could do:
#!/usr/bin/perl -w $holdspace = ""; while (<>) { if (/'1'/) { s/^/(+1) /g; s/$/;fac=1/g; $holdspace = $_; $_ = ""; } elsif (/'2'/) { s/^/(+2) /g; s/$/;fac=2/g; $_ .= $holdspace; $holdspace = $_; $_ = ""; } elsif (/'3'/) { s/^/(+3) /g; s/$/;fac=3/g; $_ = "Newline1\n" . $_; $_ .= $holdspace; $holdspace = ""; } if (/Line5/) { $_ .= "MeanLine\n"; } print; }
But I personally think that's both ugly and limited in what files it applies to.

In reply to Re: sed to perl conversion revisted by fizbin
in thread sed to perl conversion revisted by NovMonk

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.