[Top][All Lists]

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

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

From: Andrew J. Schorr
Subject: Re: [bug-gawk] -i infile unexpected behavior
Date: Sun, 15 Mar 2015 12:26:53 -0400
User-agent: Mutt/1.5.23 (2014-03-12)


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

>    $ 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
bash-4.2$ cat file
bash-4.2$ awk -i inplace 'ENDFILE {print "bar"}' file
bash-4.2$ cat file

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?

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

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.


reply via email to

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