how to write finding output to same file using awk

2019-03-09 18:16发布

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.

标签: bash shell awk
6条回答
放荡不羁爱自由
2楼-- · 2019-03-09 18:43

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 '{a[b++]=$0} END {for(c=1;c<=b;c++)print a[c]>ARGV[1]}' file
查看更多
相关推荐>>
3楼-- · 2019-03-09 18:46

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.

print $total, total >> "new_file" 
查看更多
家丑人穷心不美
4楼-- · 2019-03-09 18:48

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

$ 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 -i feature.

See: Enabling In-Place File Editing

查看更多
一纸荒年 Trace。
5楼-- · 2019-03-09 18:56

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:

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).

查看更多
孤傲高冷的网名
6楼-- · 2019-03-09 19:05

You can also use sponge from moreutils.

For example

awk '!a[$0]++' file|sponge file

removes duplicate lines and

 awk '{$2=10*$2}1' file|sponge file

multiplies the second column by 10.

查看更多
再贱就再见
7楼-- · 2019-03-09 19:08

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.

查看更多
登录 后发表回答