bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] How to just read header of files in ARGV?


From: arnold
Subject: Re: [bug-gawk] How to just read header of files in ARGV?
Date: Fri, 13 Sep 2019 01:13:10 -0600
User-agent: Heirloom mailx 12.5 7/5/10

You should read the manual. In particular the bits about FNR and
about how file redirections are totally unrelated to awk's main
processing loop.

In any case, here is how to go about it:

$ cat test.awk
FNR == 1 { print FILENAME, "header", $0 ; next }
{ print FILENAME, "body", $0 }

$ gawk -f test.awk  <(printf '%s\n' h1 {1..3}) \
>  <(printf '%s\n' h2 {11..13}) <(printf '%s\n' h3 {21..23})
/dev/fd/63 header h1
/dev/fd/63 body 1
/dev/fd/63 body 2
/dev/fd/63 body 3
/dev/fd/62 header h2
/dev/fd/62 body 11
/dev/fd/62 body 12
/dev/fd/62 body 13
/dev/fd/61 header h3
/dev/fd/61 body 21
/dev/fd/61 body 22
/dev/fd/61 body 23

Peng Yu <address@hidden> wrote:

> BEGIN {
>       for(i=1;i<ARGC;++i) {
>               getline < ARGV[i]
>               print i, $0
>       }
> }
> {
>       print FILENAME, $1
> }
>
> If I attempt to read file headers in BEGIN, I got the following error.
> What is the correct way to peek file headers yet still let awk
> automatically read the rest lines of the files? Thanks.
>
> $ awk -v FS='\t' -f .main.awk <(printf '%s\n' h1 {1..3}) <(printf
> '%s\n' h2 {11..13}) <(printf '%s\n' h3 {21..23})
> 1     h1
> 2     h2
> 3     h3
> awk: warning: close of fd 61 (`/dev/fd/61') failed (Bad file descriptor)
> awk: warning: close of fd 62 (`/dev/fd/62') failed (Bad file descriptor)
> awk: warning: close of fd 63 (`/dev/fd/63') failed (Bad file descriptor)
>
> -- 
> Regards,
> Peng



reply via email to

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