我使用awk来计算一些总和,并且我想将它们存储在一个文件中。从awk获取结果并将其打印到文件中
这里是我的输入文件:
misses 15
hit 18
misses 20
hit 31
我想打印的总失误和总点击在文件中。
如果我运行此:
awk 'BEGIN { hits=0; misses=0; } /^hit/{ hits+=$2}; /^misses/{misses+=$2}; END {print "Hits: " hits "\nMisses: " misses }' t.txt
我看到他们在终端。
现在我想写一个文件:
我尝试这样做:
#!/bin/bash
awk 'BEGIN { hits=0; misses=0; } /^hit/{ hits+=$2}; /^misses/{misses+=$2}; END {print "Hits: " hits "\nMisses: " misses }' t.txt
echo misses $misses > $1; #first one overwrites the previous $1 is the argument given in the command line
echo hits $hits>> $1; # all the othes append to the .txt file
但$misses, and $hits
没有价值。
我也试过这样:
#!/bin/bash
result= $(echo $output | awk 'BEGIN { hits=0; misses=0; } /^hit/{ hits+=$2}; /^misses/{misses+=$2}; END {print "Hits: " hits "\nMisses: " misses }' t.txt)
# $NF if I want the last column
echo $result
,我在网络上看到了,为了看什么$结果将返回我,但我得到这个错误:
./test2.sh: line 2: Hits:: command not found