awk \'/^nameserver/ && !modif { printf(\"nameserver 127.0.0.1\\n\"); modif=1 } {print}\' testfile.txt
It is displaying output but I want to wri
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 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.