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 21:13:09 -0500
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0

Andy - the script below behaved as desired in my testing. I'd never really write or advocate anyone else writing the script below or
     gawk -e '1; ENDFILE {print "bar"}' -i inplace file
though as the whole point of having
awk -i inplace '...' file
is to provide a briefer, simpler solution to write than
awk '...' file > tmp && mv tmp file
(analogous to "sed -i '...' file") and both of those are lengthier and more complicated (even for multiple files) so then what would be the point?

Thanks for the info and for looking into it though.

    Ed.

On 3/15/2015 5:38 PM, Andrew J. Schorr wrote:
Hi Ed,

On Sun, Mar 15, 2015 at 12:35:09PM -0500, Ed Morton wrote:
On 3/15/2015 11:26 AM, Andrew J. Schorr wrote:
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.
Could you move the code that terminates the inplace processing from the
ENDFILE to a BEGINFILE rule?
You're certainly free to replace inplace.awk with your own version.
For example, you could try:

   @load "inplace"

   BEGINFILE {
      if (INPLACE_FILENAME)
	  inplace_end(INPLACE_FILENAME, INPLACE_SUFFIX)
      inplace_begin(FILENAME, INPLACE_SUFFIX)
      INPLACE_FILENAME = FILENAME
   }

   END {
      inplace_end(FILENAME, INPLACE_SUFFIX)
   }

I haven't tested this, but maybe it would behave more sensibly.  If you give it
a try, please report back on your results.

Regards,
Andy



reply via email to

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