gawk: Extension Sample Inplace
1
1 16.7.4 Enabling In-Place File Editing
1 -------------------------------------
1
1 The 'inplace' extension emulates GNU 'sed''s '-i' option, which performs
1 "in-place" editing of each input file. It uses the bundled
1 'inplace.awk' include file to invoke the extension properly:
1
1 # inplace --- load and invoke the inplace extension.
1
1 @load "inplace"
1
1 # Please set INPLACE_SUFFIX to make a backup copy. For example, you may
1 # want to set INPLACE_SUFFIX to .bak on the command line or in a BEGIN rule.
1
1 # By default, each filename on the command line will be edited inplace.
1 # But you can selectively disable this by adding an inplace=0 argument
1 # prior to files that you do not want to process this way. You can then
1 # reenable it later on the commandline by putting inplace=1 before files
1 # that you wish to be subject to inplace editing.
1
1 # N.B. We call inplace_end() in the BEGINFILE and END rules so that any
1 # actions in an ENDFILE rule will be redirected as expected.
1
1 BEGIN {
1 inplace = 1 # enabled by default
1 }
1
1 BEGINFILE {
1 if (_inplace_filename != "")
1 inplace_end(_inplace_filename, INPLACE_SUFFIX)
1 if (inplace)
1 inplace_begin(_inplace_filename = FILENAME, INPLACE_SUFFIX)
1 else
1 _inplace_filename = ""
1 }
1
1 END {
1 if (_inplace_filename != "")
1 inplace_end(_inplace_filename, INPLACE_SUFFIX)
1 }
1
1 For each regular file that is processed, the extension redirects
1 standard output to a temporary file configured to have the same owner
1 and permissions as the original. After the file has been processed, the
1 extension restores standard output to its original destination. If
1 'INPLACE_SUFFIX' is not an empty string, the original file is linked to
1 a backup file name created by appending that suffix. Finally, the
1 temporary file is renamed to the original file name.
1
1 Note that the use of this feature can be controlled by placing
1 'inplace=0' on the command-line prior to listing files that should not
1 be processed this way. You can reenable inplace editing by adding an
1 'inplace=1' argument prior to files that should be subject to inplace
1 editing.
1
1 The '_inplace_filename' variable serves to keep track of the current
1 filename so as to not invoke 'inplace_end()' before processing the first
1 file.
1
1 If any error occurs, the extension issues a fatal error to terminate
1 processing immediately without damaging the original file.
1
1 Here are some simple examples:
1
1 $ gawk -i inplace '{ gsub(/foo/, "bar") }; { print }' file1 file2 file3
1
1 To keep a backup copy of the original files, try this:
1
1 $ gawk -i inplace -v INPLACE_SUFFIX=.bak '{ gsub(/foo/, "bar") }
1 > { print }' file1 file2 file3
1
1 Please note that, while the extension does attempt to preserve
1 ownership and permissions, it makes no attempt to copy the ACLs from the
1 original file.
1
1 If the program dies prematurely, as might happen if an unhandled
1 signal is received, a temporary file may be left behind.
1