linux向文件追加行并求和

使用awk函数,-F用逗号分隔,"\t"换行

awk -F "\t" '{sum += $[列数]};END {print sum}' filename >[输出文件]

$后面为列数,>后面为输出文件名,“>”符号为输出文件,“>>”为追加

例如,

1.将table文件夹下的table.txt文件第2列求和并输出到sum文件

awk -F "\t" '{sum += $2};END {print sum}' table/table.txt >sum

 2.向sum文件追加内容

echo [追加内容] >> file_to_append_to
echo "Hello" >>sum

 3.向sum文件追加输出内容

command >> file_to_append_to
awk -F "\t" '{sum += $3};END {print sum}' table/table.txt >>sum
awk -F "\t" '{sum += $4};END {print sum}' table/table.txt >>sum
awk -F "\t" '{sum += $5};END {print sum}' table/table.txt >>sum

4.求sum文件中的最小值及最大值并求和

#最小值
cut -f1 -d"," sum | sort -n | head -1 
#最大值
cut -f1 -d"," sum | sort -n | tail -1 

#求和
awk '{sum += $1};END {print sum}' sum  

Reference:

linux对文件某列求和 - - ITeye博客

(2条消息) awk 列求和计算_Congying-Wang的博客-CSDN博客_awk相同列求和

linux - Finding the max and min values and printing the line from a file - Stack Overflowhttps://stackoverflow.com/questions/16212410/finding-the-max-and-min-values-and-printing-the-line-from-a-file

bash - How to append output to the end of a text file - Stack Overflowhttps://stackoverflow.com/questions/6207573/how-to-append-output-to-the-end-of-a-text-file

你可能感兴趣的:(Linux,r语言,网络,数据库)