Save modifications in place with awk

In GNU Awk 4.1.0 (released 2013) and later, it has the option of “inplace” file editing:

[…] The “inplace” extension, built using the new facility, can be used to simulate the GNU “sed -i” feature. […]

Example usage:

$ gawk -i inplace '{ gsub(/foo/, "bar") }; { print }' file1 file2 file3

To keep the backup:

$ gawk -i inplace -v INPLACE_SUFFIX=.bak '{ gsub(/foo/, "bar") }
> { print }' file1 file2 file3

Leave a Comment