Save modifications in place with awk

前端 未结 8 1968
轻奢々
轻奢々 2020-11-21 23:32

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

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 23:53

    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
    

提交回复
热议问题