如何使用awk命令写找到输出到同一个文件 [英] how to write finding output to same file using awk command

查看:405
本文介绍了如何使用awk命令写找到输出到同一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是显示输出,但我想写输出到同一个文件。在我的例子 TESTFILE.TXT

推荐答案

不可能本身。你需要一个第二次临时文件,因为你不能阅读和覆盖相同的文件。是这样的:

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

mktemp的程序生成唯一的临时文件名是有用的。

The mktemp program is useful for generating unique temporary file names.

有一些黑客避免临时文件,但它们主要依靠缓存和缓冲区读取并迅速得到不稳定的更大的文件。

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.

这篇关于如何使用awk命令写找到输出到同一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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