help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Printing arbitrary line range from files


From: Dennis Williamson
Subject: Re: Printing arbitrary line range from files
Date: Mon, 28 Jun 2021 13:39:46 -0500

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]