cat foofile | perl -ne 'if (/\[foo\]/){$print++;next};next if /\[foo2\]/;print if ($print)'