Purely In The Interests Of Science, I present an implementation of torso, the logical middle between head and tail.
In practice, as others have noted, this is really unnecessary since you can get the desired output yourself by a trivial combination of head and tail.
#!/bin/sh
usage () {
printf "$0: $0 [-c <byte> -C <byte>] [-n <line> -N <line>] file [file ... ]\n"
}
while [ $# -gt 0 ] ; do
case "$1" in
-c|--byte-start) shift ; start="$1" ; mode=byte ; shift ;;
-C|--byte-end) shift ; end="$1" ; mode=byte ; shift ;;
-n|--line-start) shift ; start="$1" ; mode=line ; shift ;;
-N|--line-end) shift ; end="$1" ; mode=line ; shift ;;
--) shift ;;
-*) printf "bad option '%s'\n" "$1" ; usage ; exit 201 ;;
*) files=("${files[@]}" "$1") ; shift ;;
esac
done
if [ $start -gt $end ] ; then
printf "end point cannot be before start point\n"
usage
exit 202
fi
head_cmd=
tail_cmd=
end=$((end - start))
if [ $mode = "line" ] ; then
head_cmd="-n $end"
tail_cmd="-n +$start"
elif [ $mode = "byte" ] ; then
head_cmd="-c $end"
tail_cmd="-c +$start"
fi
if [ ${#files[@]} -eq 0 ] ; then
cat - | tail $tail_cmd | head $head_cmd
else
tail $tail_cmd "${files[@]}" | head $head_cmd
fi
To keep it topical, here's how to use torso to solve the question:
torso -n 1500 -N 2500 input_file | grep -n "test"
Or for output conforming to the requirements
for file in sample_{1,2,7,10} ; do
printf "\n\n%s:\n\n" "$file"
torso -n 1500 -N 2500 "$file" | grep -n "test"
done
You may begin your criticisms... now!