Deano Deano - 9 months ago 86
Linux Question

awk save modifications inplace

I am learning

and I would like to know if there is an option to write changes to file, similar to
where I would use
option to save modifications to a file.

I do understand, that I could use redirection to write changes, however is there is such option to do that?


In latest GNU Awk (since 4.1.0 released), 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