Deano Deano - 3 months ago 29
Linux Question

awk save modifications inplace

I am learning

awk
and I would like to know if there is an option to write changes to file, similar to
sed
where I would use
-i
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?

Answer

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
Comments