#!/bin/sh if [ "_$3" = "_" ];then count=1 else count="$3" fi awk ' BEGIN {counter='$count'} /'$1'/, /'$2'/{ print $0; if($0 ~ /'$2'/) { counter--; } if(counter == 0) {nextfile} } '