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
.
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
.
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:
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.
Since GNU Awk 4.1.0, there is the "inplace" extension, so you can do:
To keep a backup copy of original files, try this:
This can be used to simulate the GNU
sed -i
feature.See: Enabling In-Place File Editing
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)
So my method to avoid temp file is simple:
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).
You can also use
sponge
from moreutils.For example
removes duplicate lines and
multiplies the second column by 10.
Not possible per se. You need a second temporary file because you can't read and overwrite the same file. Something like:
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.