bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] What is wrong with getline from both stdin and a file fro


From: Peng Yu
Subject: Re: [bug-gawk] What is wrong with getline from both stdin and a file from ARGV?
Date: Wed, 28 Nov 2018 22:22:56 -0600

On Wed, Nov 28, 2018 at 8:07 PM Andrew J. Schorr
<address@hidden> wrote:
>
> On Wed, Nov 28, 2018 at 07:51:28PM -0600, Peng Yu wrote:
> > > The problem with your second command, as I already explained, is that it 
> > > opens
> > > the file twice: the first time because it's supplied on the command-line 
> > > as a
> > > source of data, and the second time because you explicitly accessed it by
> > > calling "getline < ARGV[1]".
> >
> > I am not sure this explanation makes sense.
>
> Trust me -- it makes sense. You have confused yourself because
> stdin is irrelevant in your example. Please go back and reread my last
> message slowly...
>
> > Why the first getline in the following example read from /dev/fd/63?
> > It is to read from stdin, shouldn't it be different from ARGV[1]?
> >
> > $ printf '%s\n' 1 2 3 | awk -e 'BEGIN { getline; print getline <
> > ARGV[1]; print  }' <(printf '%s\n' {a..c})
> > 0
> > a
> > awk: (FILENAME=/dev/fd/63 FNR=1) warning: close of fd 63
> > (`/dev/fd/63') failed (Bad file descriptor)
>
> No -- it does not read from stdin, because you supplied a data file
> on the command line. If there's a file on the command-line, then stdin
> is ignored. For example:
>
> bash-4.2$ echo hello | awk -F: 'NR == 1 {print FILENAME, $1}' /etc/passwd
> /etc/passwd root
>
> Do you understand now?
>
> > Also, the following code works just fine. It would be strange to be
> > able to read ARGV[2] but not ARGV[1]
> > $ printf '%s\n' 1 2 3 | awk -e 'BEGIN { getline; print getline <
> > ARGV[2]; print  }' <(printf '%s\n' {a..c})  <(printf '%s\n' {A..C})
>
> Try printing out the data returned by your getline calls, and then maybe you
> will understand.

I see what I was wrong. `getline` will read from ARGV[1] once it has
been set, even later on ARGV is deleted.

$ printf '%s\n' {1..3} | mawk.sh -e 'BEGIN { getline; print; getline <
ARGV[2]; print; delete ARGV; getline; print }' <(printf '%s\n' {a..c})
<(printf '%s\n' {A..C})
printf '%s\n' {A..C}
printf '%s\n' {a..c}
a
A
b

-- 
Regards,
Peng



reply via email to

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