bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] -i infile unexpected behavior


From: Ed Morton
Subject: Re: [bug-gawk] -i infile unexpected behavior
Date: Sun, 15 Mar 2015 11:43:28 -0500
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0

Hi,

On 3/15/2015 11:26 AM, Andrew J. Schorr wrote:
Hi,

On Sun, Mar 15, 2015 at 11:19:16AM -0500, Ed Morton wrote:
I don't believe this is the way `-i infile`
(http://www.gnu.org/software/gawk/manual/gawk.html#Extension-Sample-Inplace)
should work:

   $ cat file
   foo
   $ awk -i infile 'END{print "bar"}' file
   bar
What does "-i infile" mean?  The examples in the documentation say "-i inplace".
Have you made your own local modifications to implement an "infile" include
mechanism?
Sorry, I used the wrong word when trying to create the test case. Here's the real use case:
$ cat file
foo
$ awk -i inplace '{print} ENDFILE{print "bar"}' file
bar
$ awk -i inplace '{print} END{print "bar"}' file
bar
$ cat file
foo
   $ awk -i infile 'ENDFILE{print "bar"}' file
   bar
   $ cat file
   foo

I expected "bar" to get written to "file" in both the `END` and `ENDFILE`
conditions, not printed to stdout. I'm on cygwin:
Here is what I see:

bash-4.2$ echo foo > file
bash-4.2$ awk -i inplace 'END {print "bar"}' file
bar
bash-4.2$ cat file
bash-4.2$ awk -i inplace 'ENDFILE {print "bar"}' file
bar
bash-4.2$ cat file
bash-4.2$ 

Please consider that "inplace" must be able to work when multiple
files are on the command line.  Is it your expectation that the "END"
rule should apply to the last file?
I fully understand that. My expectation is that a print in the END section would write to the last file read and more importantly I would expect `-i inplace ... ENDFILE` to behave like:
$ cat file
foo
$ sed -i '$abar' file
$ cat file
foo
bar

The "inplace" include file contains an ENDFILE rule that terminates the
inplace processing.  Consider an alternative approach:

bash-4.2$ awk 'ENDFILE {print "bar"}; @include "inplace"' file
bash-4.2$ cat file
bar
bash-4.2$ 

This includes the "inplace" ENDFILE rule after yours, so that the
print occurs before the "inplace" processing is terminated.
In other words, the ordering of the ENDFILE rules matters.
That is very counter-intuitive don't you think? I think the expectation would simply be that `-i inplace` behaves like seds `-i`.

    Ed.

Regards,
Andy



reply via email to

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