AWK
1 Some basics
awk '{print $5}' file # 打印文件中以空格分隔的第五列
awk -F ',' '{print $5}' file # 打印文件中以逗号分隔的第五列
awk '/str/ {print $2}' file # 打印文件中包含 str 的所有行的第二列
awk -F ',' '{print $NF}' file # 打印逗号分隔的文件中的每行最后一列
awk '{s+=$1} END {print s}' file # 计算所有第一列的合
awk 'NR%3==1' file # 从第一行开始,每隔三行打印一行
2 Improved
data.txt
cat data.txt
1
2
3
2.1 Calculate sum
cat sum.sh
#!/bin/bash
cat data.txt | awk 'BEGIN{sum=0;} {sum+=$1;} END{print sum;}'
./sum.sh
6
2.2 Calculate average
cat average.sh
#!/bin/bash
cat data.txt | awk 'BEGIN{sum=0;} {sum+=$1;} END{print sum/NR;}'
./average.sh
2
2.3 Calculate variance
cat variance.sh
#!/bin/bash
cat data.txt | awk 'BEGIN {n=0; sum=0; ss=0;} \
{x[NR]=$1; n++; sum+=$1;} \
END { \
average=sum/NR; \
for (i=1; i<=n; ++i) \
{ss+=(x[i]-average)^2;} \
print ss/NR; \
}'
./variance.sh
0.666667
2.4 Calculate geomean
cat geomean.sh
#!/bin/bash
cat data.txt | awk 'BEGIN {prod = 1;} \
{prod *= $1;} \
END {print prod ^ (1/NR);}'
./geomean.sh
1.81712