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: Andrew J. Schorr
Subject: Re: [bug-gawk] -i infile unexpected behavior
Date: Sun, 15 Mar 2015 22:56:00 -0400
User-agent: Mutt/1.5.23 (2014-03-12)

On Sun, Mar 15, 2015 at 09:13:09PM -0500, Ed Morton wrote:
> 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?

Well, the revision I provided does help a bit:

bash-4.2$ echo foo > file
bash-4.2$ cat /tmp/Inplace.awk 
# inplace --- load and invoke the inplace extension.

@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)
}

bash-4.2$ awk -i /tmp/Inplace '1; ENDFILE {print "bar"}' file
bash-4.2$ cat file
foo
bar
bash-4.2$ awk -i /tmp/Inplace '1; END {print "bar"}' file
bar
bash-4.2$ cat file
foo
bar
bash-4.2$ 

It solves the "ENDFILE" problem, but does not fix the "END" case.  The
disadvantage is that we have to introduce a new global variable to
store the filename.

Is it worth making this change?

Regards,
Andy



reply via email to

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