From: Dennis Williamson <dennistwilliamson@gmail.com>
To: help-bash <help-bash@gnu.org>
Subject: Re: Printing arbitrary line range from files
Date: 28/06/2021 20:39:46 Europe/Paris
On Mon, Jun 28, 2021, 12:49 PM Greg Wooledge <greg@wooledge.org> wrote:
> On Mon, Jun 28, 2021 at 07:25:37PM +0200, lisa-asket@perso.be wrote:
> > Would like as below
> >
> > ==> foo <==
> >
> > b
> > c
> > d
> >
> > ==> bar <==
> >
> > 2
> > 3
> > 4This is similar to results using `head`.
>
> This is why we want the full problem definition up front. Piecemeal
> modifications always lead to multiple complete rewrites. It's VERY
> frustrating.
>
> Here's one way, using GNU or BSD find's -print0 action to feed a
> machine-readable list of pathnames into a while-read loop:
>
> find "$3" \( ... \) -print0 |
> while IFS= read -r -d '' file; do
> printf '==> %s <==\n' "$file"
> sed -n "$1,${2}p" "$file"
> done
>
Modifying one of your earlier awk-only versions:
awk -v a=2 -v b=4 'FNR == 1 { print endfile "==> " FILENAME " <==\n";
endfile = "\n"} FNR >= a && FNR <= b {print}' foo bar
* The advantage of using awk is that unlike sed, awk accepts multiple files.