in reply to sed to perl conversion revisted
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 (/'(\d)'/) { s/^/(+$1) /g; s/$/;fac=$1/g; unshift @holdspace, $_; } else { if (@holdspace) { print "Newline1\n", @holdspace; @holdspace=(); } print; } if (/Line5/) { print "MeanLine\n"; } }
But I personally think that's both ugly and limited in what files it applies to.#!/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; }
|
|---|