Esteemed Monks,

In my searches of regexp and Perl, I am not finding the answer to this-- is it possible to flip the order of lines based on their matching a given pattern? What I have in mind is this-- you have a file with the follwing contents:

1. text'1' 2. text'2' 3. text'3' ...

With sed, you could reverse the order of the lines thus:

/'1'/{ h d } /'2'/G /'2'/{ h d } /'3'/G /'3'/{ h d }

and end up with the file

3.text'3' 2.text'2' 1.text'1'

Is there a similar capability with Perl? I was working earlier today on making a program interactive with vi (see my earlier question), and this is a similar thing-- I need it to do this within a "while" construction and spit out all the lines back into the file I'm editing when I'm done. Can this be done?

Thanks.


In reply to Sed to perl conversion-- flipping lines in a file 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.