You could do this way also:
a) This is
POSIX-sed compliant code wherein we store the mid-range lines (!/foo/ and !/bar/) in the hold area.
sed -e '
b) and with
Perl as shown
perl -lne '
next unless /foo/ ... /bar/;
push(@A, $_),next if !/foo/ && !/bar/ && !eof;
push(@A, $_) if !/foo/ && !/bar/ && eof;
print join " ", splice @A if /bar/ || !/foo/ && eof;
- skip non-interesting lines or lines that fall out of range. A range is defined as beginning with a line containing
/foo/ and ending with
/bar/, both occurring on separate lines.
- Once we are in the range, then perform separate actions based on what line we are in.
- For lines that are not /foo/, neither /bar/, and neither eof : store the line in the array
@A. After storing, go back for reading the next record provided not eof.
- Only for the end of range, i.e.,
eof, we take the action of printing what's in
@A and also emptying it, in preparation for the next gathering cycle.