Greetings, monks.

As several monks have kindly pointed out my question about sed was not clear, therefore I revise it here. Even though it is a very simple problem, bear in mind that

I am very new and clueless

and

I've benefited a great deal from the simple suggestions made to me thus far.

I want to be able to use perl interactively with vi to achieve the following. If the file has this:

Line1 Line2 Line3 Line ‘1’ Line ‘2’ Line ‘3’ Line4 Line5

I want it to ultimately look like this:

Line1 Line2 Line3 NewLine1 (+3) Line ’3’;fac=3 (+2) Line ‘2’;fac=2 (+1) Line ‘1’;fac=1 Line4 Line5 MeanLine

So far, I can get this if the file is already in the Order of the second file—what I want it to do is flip those 3 lines before applying the other changes. The script I have right now looks like this:

#!/usr/bin/perl -w while (<>) { if (/'3'/) { s/^L/(+3) L/g; s/'3'/'3';fac=3/g; s/^/Newline1\n/g } if (/'2'/) { s/^L/(+2) L/g; s/'2'/'2';fac=2/g; } if (/'1'/) { s/^L/(+1) L/g; s/'1'/'1';fac=1/g; } if (/Line5/) { s/Line5/Line5\nMeanLine/g } print "$_"; }

I had a sed script that would reverse the order of the lines based on their matching a pattern-- what I'm trying to find is a Simple way to achieve this same result. While substitution looks pretty much the same as sed in perl, I can't seem to find anything like sed's ability to put stuff in a hold space and retrieve it later, like this sed does:

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

Putting this through s2p gave me 3 pages of code I won't understand until saint level.

I'm also trying to get it to do what the program above does-- work over a block of text in a file being edited with vi editor, and invoked thus:

:1,17! perlprogram

Thanks in advance for your help.


In reply to 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.