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: Greg Wooledge
Subject: Re: Printing arbitrary line range from files
Date: Mon, 28 Jun 2021 12:42:53 -0400

On Mon, Jun 28, 2021 at 06:10:34PM +0200, lisa-asket@perso.be wrote:
> print-lines() {
> if (($# != 3)); then
> echo 'usage: print-lines startnum endnum startdir' >&2
> return 1
> fi
> 
> find "$3" \( ... \) \
> -exec awk -v a="$1" -v b="$2" 'FNR >= a && FNR <= b' {} +
> }

This has gotta be some sort of web-based MUA.  It destroyed all
the indentation. :(

How do people *use* these things?

> * Your awk command does as I wish.  How can I add the file name before each
> output?   

  2. The questioner will keep changing the original question until it
     drives the helpers in the channel insane.

Now I'm wondering what "each output" means.  Each line?  Each file's
block of lines?

Here's an answer to one of those:

unicorn:~$ awk -v a=2 -v b=4 'FNR >= a && FNR <= b {print FILENAME ": " $0}' 
foo bar
foo: b
foo: c
foo: d
bar: 2
bar: 3
bar: 4

Of course, this means you will want the *other* one.

  21. If^H^HWhen the newbie's question is ambiguous, the proper
      interpretation will be whichever one makes the problem the hardest
      to solve.



reply via email to

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