我使用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