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: Andrew J. Schorr
Subject: Re: [bug-gawk] What is wrong with getline from both stdin and a file from ARGV?
Date: Wed, 28 Nov 2018 19:22:19 -0500
User-agent: Mutt/1.5.21 (2010-09-15)

On Wed, Nov 28, 2018 at 04:27:40PM -0600, Peng Yu wrote:
> > Because the first getline reads from /dev/fd/63, and then when
> > you call "getline < ARGV[1]", it accesses /dev/fd/63 again. When the
> > program exits, it tries to close all open files. First, it closes
> > the file on the command line /dev/fd/63, and then it closes
> > fd 63 again because you explicitly opened it using the "getline < ARGV[1]"
> > syntax. Note that the pipe on stdin is a distraction and has no
> > impact on the results.
> 
> strace is from Linux. I currently use Mac so I can not try it.

>From some quick googling, it appears that MacOS has dtrace and dtruss
that may provide equivalent functionality. In any case, strace is not
necessary. It just helps to understand what you are doing wrong.

> But why
> the third way works. It also uses the same file handle.
> 
> $ printf '%s\n' {1..3} | awk -v f=<(printf '%s\n' {a..c}) -e 'BEGIN {
> print f; getline; print; getline < f; print }'
> /dev/fd/63
> 1
> a

Let's go back to basics. When you give awk a file on the command line, then
it uses that for data input. Otherwise, it reads data from standard input.
Do you understand the difference between these?

bash-4.2$ awk -F: 'NR == 1 {print FILENAME, $1}' /etc/passwd
/etc/passwd root
bash-4.2$ awk -F: 'NR == 1 {print FILENAME, $1}' <(cat /etc/passwd)
/dev/fd/63 root
bash-4.2$ awk -F: 'NR == 1 {print FILENAME, $1}' < /etc/passwd
- root
bash-4.2$ cat /etc/passwd | awk -F: 'NR == 1 {print FILENAME, $1}'
- root

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]". The third way does not do that. It opens the file
only once when it calls "getline < f". Can you not see the difference?

By the way, the issues you are raising are not bugs in gawk. Maybe it
would make more sense for you to ask for help in comp.lang.awk. These are
basic awk language issues.

Regards,
Andy



reply via email to

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