bash 如何使用 awk 命令将查找输出写入同一文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8019617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:09:48  来源:igfitidea点击:

how to write finding output to same file using awk command

bashshellawk

提问by Venkat

awk '/^nameserver/ && !modif { printf("nameserver 127.0.0.1\n"); modif=1 } {print}' testfile.txt

It is displaying output but I want to write the output to same file. In my example testfile.txt.

它正在显示输出,但我想将输出写入同一个文件。在我的例子中testfile.txt

回答by thiton

Not possible per se. You need a second temporary file because you can't read and overwrite the same file. Something like:

本身是不可能的。您需要第二个临时文件,因为您无法读取和覆盖同一个文件。就像是:

awk '(PROGRAM)' testfile.txt > testfile.tmp && mv testfile.tmp testfile.txt

The mktempprogram is useful for generating unique temporary file names.

mktemp程序可用于生成唯一的临时文件名。

There are some hacks for avoiding a temporary file, but they rely mostly on caching and read buffers and quickly get unstable for larger files.

有一些避免临时文件的技巧,但它们主要依赖缓存和读取缓冲区,并且对于较大的文件很快就会变得不稳定。

回答by kenorb

Since GNU Awk 4.1.0, there is the "inplace" extension, so you can do:

从 GNU Awk 4.1.0 开始,就有了“就地”扩展,所以你可以这样做:

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

To keep a backup copy of original files, try this:

要保留原始文件的备份副本,请尝试以下操作:

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

This can be used to simulate the GNU sed -ifeature.

这可用于模拟 GNUsed -i功能。

See: Enabling In-Place File Editing

请参阅:启用就地文件编辑

回答by Sylock

Despite the fact that using a temp file is correct, I don't like it because :

尽管使用临时文件是正确的,但我不喜欢它,因为:

  • you have to be sure not to erase another temp file (yes you can use mktemp - it's a pretty usefull tool)

  • you have to take care of deleting it (or moving it like thiton said) INCLUDING when your script crash or stop before the end (so deleting temp files at the end of the script is not that wise)

  • it generate IO on disk (ok not that much but we can make it lighter)

  • 您必须确保不要删除另一个临时文件(是的,您可以使用 mktemp - 这是一个非常有用的工具)

  • 您必须注意删除它(或像 thiton 所说的那样移动它),包括脚本在结束前崩溃或停止时(因此在脚本末尾删除临时文件并不是那么明智)

  • 它在磁盘上生成 IO(确定没有那么多,但我们可以让它更轻)

So my method to avoid temp file is simple:

所以我避免临时文件的方法很简单:

my_output="$(awk '(PROGRAM)' source_file)"
echo "$my_output" > source_file

Note the use of double quotes either when grabbing the output from the awk command AND when using echo (if you don't, you won't have newlines).

请注意在从 awk 命令获取输出时和在使用 echo 时使用双引号(如果不这样做,则不会有换行符)。

回答by Lri

You can also use spongefrom moreutils.

您也可以使用spongefrom moreutils

For example

例如

awk '!a[
 awk '{=10*}1' file|sponge file
]++' file|sponge file

removes duplicate lines and

删除重复的行和

awk '{a[b++]=
print $total, total >> "new_file" 
} END {for(c=1;c<=b;c++)print a[c]>ARGV[1]}' file

multiplies the second column by 10.

将第二列乘以 10。

回答by Hawk

Had to make an account when seeing 'awk' and 'not possible' in one sentence. Here is an awk-only solution without creating a temporary file:

在一句话中看到“awk”和“不可能”时必须记帐。这是一个仅使用 awk 的解决方案,无需创建临时文件:

##代码##

回答by debi

Try to include statement in your awk file so that you can find the output in a new file. Here total is a calculated value.

尝试在 awk 文件中包含语句,以便您可以在新文件中找到输出。这里的总数是一个计算值。

##代码##