Can any one help me in sorting this out in sed/awk/perl
Input file
Start variable 1
End variable 2
A
Z
Next
Start variable b
End variable a
X
Y
Next
...
...
...
Start variable m
End variable n
M
N
Next
If a pattern match End found, swap with its consecutive line
Output file :
Start variable 1
A
End variable 2
Z
Next
Start variable b
X
End variable a
Y
Next
...
...
...
Start variable m
M
End variable n
N
Next
Simple Perl solution.
$nstands for “now”,$bfor “before”, i.e. “the previous line was End”.For the updated version of your question, just add
$lto remember the line to be printed:Or, equivalen but shorter: