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