Following David C. Rankin solution, but more precise.
Here is an awk
solution that does not overwrite, but moves a section.
script.awk
svSctn!=1;
/^hello/ {svSctn++;next}
/^box/,/^floor/ {
print;
if ($0 ~ /^floor/) {printf("%s", mvdStr);svSctn++;}
next;
}
svSctn == 1 {mvdStr = mvdStr $0 ORS}
input.txt
...l1
...l2
hello
door
....l3
....l4
box
wall
floor
....l5
runing:
awk -f script.awk input.txt
output:
...l1
...l2
hello
box
wall
floor
door
....l3
....l4
....l5
explanation:
savedSection != 1 { # for every line, not in saved section (Initially savedSection=0)
print; # print the current line
}
/^hello/ { # when foudn the line containing "Hello" (already printed)
savedSection++; # Mark this line and following are in savedSection=1
next; # skip processing to read next line
}
/^box/,/^floor/ { # when in ranage section between and including "box" to "floor"
print; # print the current line
if ($0 ~ /^floor/) { # if current line "floor" (already printed)
printf("%s", movedSection); # insert here the skipped and saved section
savedSection++; # Mark this is in savedSection=2, so print the rest of line.
}
next; # skip processing to read next line (do not add this range into savedSection)
}
savedSection == 1 { # this is saved section.
movedSection = movedSection $0 ORS; # do not print, just append line to movedSection variable
}