help-bash
[Top][All Lists]
Advanced

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

Printing arbitrary line range from files


From: lisa-asket
Subject: Printing arbitrary line range from files
Date: Mon, 28 Jun 2021 19:25:37 +0200 (CEST)



From: Greg Wooledge <greg@wooledge.org>
To: help-bash@gnu.org
Subject: Re: Printing arbitrary line range from files
Date: 28/06/2021 18:42:53 Europe/Paris

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.


* Sounds serious.  I asked because when I use head, the filename is displayed. 


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.

* Genius, how did you figure that out,

Would like as below

==> foo <==

b
c
d

==> bar <==

2
3
4This is similar to results using `head`.


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]