图片来源:https://www.pixiv.net/artworks/89845503
有如下三个表格,他们各自对应一个CSV格式文件
Fruit | Qty | Price |
Apple | 13 | 1.1 |
Banana | 24 | 2.3 |
Pear | 7 | 1.5 |
Fruit,Qty,Price
Apple,13,1.1
Banana,24,2.3
Pear,7,1.5
Fruit | Qty | Price |
Apple | 27 | 5.5 |
Pear | 40 | 3.04 |
Watermelon | 5 | 0.99 |
Fruit,Qty,Price
Apple,27,5.5
Pear,40,3.04
Watermelon,5,0.99
Fruit | Qty | Price |
Banana | 12 | 0.55 |
Pear | 15 | 2.98 |
Peach | 8 | 6 |
Fruit,Qty,Price
Banana,12,0.55
Pear,15,2.98
Peach,8,6
使用awk合并上述3个表格,对于相同水果合并数量并求均价
#!/usr/bin/awk -f
BEGIN{
#用“,"作为分隔符切割
FS=","
}
{
if($1 != "Fruit"){
fruits[$1] = $1 #水果名称
qty[$1] += $2 #水果数量
price[$1] += $2*$3 #水果总价,输出时用来求均价
}
}
END{
# 格式化打印表头
printf("%-10s %-10s %-10s\n", "Fruit", "Qty", "Avg Price")
for(k in fruits){
#格式化输出水果名、水果数量、水果均价
printf("%-10s %-10d %-8.2f\n", k,qty[k], price[k]/qty[k])
}
}
用法:
./form_merge.awk shop*.csv
# 或者
awk -f form_merge.awk shop*.csv
输出:
Fruit Qty Avg Price
Peach 8 6.00
Watermelon 5 0.99
Banana 36 1.72
Pear 62 2.85
Apple 40 4.07
合并后输出为CSV格式的单行版本
awk -F ',' '{if($1=="Fruit")t=$0;else{f[$1]=$1;q[$1]+=$2;p[$1]+=$2*$3}}END{print t;for(k in f)print k","q[k]","p[k]/q[k]}' shop*.csv
输出:
Fruit,Qty,Price
Peach,8,6
Watermelon,5,0.99
Banana,36,1.71667
Pear,62,2.85161
Apple,40,4.07
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。