AWK:更换和写入输入文件中的列值 [英] AWK: replace and write a column value in the input file
问题描述
我想如果$ 3更换输入文件的$ 3栏值> 100。我想:
I am trying to replace the $3 column values of the input file if $3 is > 100. I tried:
awk 'BEGIN {FS="\t"} {if($3 > 100) $3=$3/100;print}' test.stat
这输出到标准输出正确的变化,但是,我需要的变化写入输入文件(test.stat),这样,剩下的字段的值/记录保持不变。任何建议?
This outputs the correct changes to the stdout but, I need the change to write to the input file (test.stat) such that, the values of the remaining fields/records remain unchanged. Any suggestion?
感谢。另一个问题已经出现。我有其总和需要在END块要打印一个反变量,我想:
Thanks. Another issue has arisen. I have a "counter" variable whose sum needs to be printed in the END block, I tried:
awk 'BEGIN {FS="\t",counter=0}
{if($3 > 100) $3=$3/100;print else counter++}
END{print counter}' test.stat > ...
现在,只有计数器值被写入到该文件,而不是$ 3个值。我怎样才能分出两个输出因此一个修改文件,另一个保存为bash的变量使用读命令。谢谢你。
Now, only the counter value gets written to the file and not the $3 values. How can I separate the two outputs so one modifies the file and the other saved as bash variable using read command. Thanks.
推荐答案
awk的不是设计就地编辑的东西。它被设计用来处理数据并写入到stdout(或其他文件)。你可以做这样的事情:
Awk isn't designed to edit things in-place. It's designed to process data and write it to stdout (or another file). You can do something like this:
$ awk 'BEGIN {FS="\t"} {if($3 > 100) $3=$3/100;print}' test.stat > test.stat.new \
&& mv test.stat test.stat.old && mv test.stat.new test.stat
这篇关于AWK:更换和写入输入文件中的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!