Scott McClanahan wrote: > grep out the next 5 lines after the first and only instance The scope of grep's view of the world is a single line. At any one time, it knows nothing more. If you need to deal with multiple lines, I suggest perl. Untested code: #!/usr/bin/perl while (<>) { if (m/bar/) { for ($i = 0; $i < 5 and defined($_); ++$i) { print; $_ = <>; } last; } }