how to write finding output to same file using awk command

前端 未结 6 1502
无人及你
无人及你 2021-01-31 15:46
awk \'/^nameserver/ && !modif { printf(\"nameserver 127.0.0.1\\n\"); modif=1 } {print}\' testfile.txt

It is displaying output but I want to wri

6条回答
  •  春和景丽
    2021-01-31 16:31

    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.

提交回复
热议问题