NovMonk has asked for the wisdom of the Perl Monks concerning the following question:
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 filewhat 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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sed to perl conversion revisted
by Roger (Parson) on Feb 20, 2004 at 22:33 UTC | |
|
Re: sed to perl conversion revisted
by Skeeve (Parson) on Feb 20, 2004 at 22:27 UTC | |
|
Re: sed to perl conversion revisted
by fizbin (Chaplain) on Feb 20, 2004 at 22:48 UTC | |
|
Re: sed to perl conversion revisted
by ambrus (Abbot) on Feb 20, 2004 at 22:43 UTC | |
|
Re: sed to perl conversion revisted
by ambrus (Abbot) on Feb 20, 2004 at 22:28 UTC |