bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] in-place edit request


From: Andrew J. Schorr
Subject: Re: [bug-gawk] in-place edit request
Date: Wed, 26 Dec 2012 14:19:17 -0500
User-agent: Mutt/1.5.21 (2010-09-15)

Hi Eli,

On Wed, Dec 26, 2012 at 09:03:32PM +0200, Eli Zaretskii wrote:
> Sorry, I still don't see the light.  If all you want is let Gawk use
> stdout "like it normally does", why not just use it?  Or just use
> freopen to redirect stdout to a temporary file, then mv that file as
> you intended?

It is easy to redirect stdout using freopen or dup2.  The difficult part
is restoring it after processing the input file.  This is difficult
if one uses freopen and requires the /dev/fd trick.  That is why
I prefer the dup2 solution.

Why do we want to restore stdout?  This may be required when processing
multiple input files or when some print statements occur in a context where an
input file is not being processed.  For example, consider this program:

gawk -i inplace '{print gensub(/foo/, "bar", "g")} END {print "all done"}' 
/tmp/a /tmp/b

The final "all done" message should go to the terminal, not into the
replacement new version of /tmp/b.  So we need to be able to restore the
default stdout destination.

Note that our proposed solution isn't perfect, since we can't guarantee the
ordering when multiple ENDFILE rules are present.  My guess is that we
shouldn't worry about that case, since I think the intent is to enable simple
one-liners to work properly.

> I guess I don't understand what is meant by "in-place edit".  The
> explanation was by reference to Perl, which doesn't help me at all.

The man pages for perl and sed both explain the "-i" option.  The system
call traces I emailed previously also show what it does.  For example,
here is the sed man page entry:

        -i[SUFFIX], --in-place[=SUFFIX]
                edit files in place (makes backup if extension supplied)

Regards,
Andy



reply via email to

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