使用 awk 将修改保存到位 [英] Save modifications in place with awk

查看:42
本文介绍了使用 awk 将修改保存到位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 awk,我想知道是否有一个选项可以将更改写入文件,类似于 sed,我将在其中使用 -i 选项来保存对文件的修改.

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.

我知道我可以使用重定向来写入更改.但是在 awk 中有一个选项可以做到这一点吗?

I do understand that I could use redirection to write changes. However is there an option in awk to do that?

推荐答案

在 GNU Awk 4.1.0(2013 年发布)及更高版本,它可以选择 就地"文件编辑:

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

[...] 就地"使用新工具构建的扩展可用于模拟 GNU sed -i"特征.[...]

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

示例用法:

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

要保留备份:

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

这篇关于使用 awk 将修改保存到位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆