我有以下文件calories
:
Apple pie s s n
Lemon pie s n n
Orange pie s s s
我有一个 awk 脚本,可以打印 s(sugar) 出现的最大次数的饼图。
#!/bin/awk -f
BEGIN{ sugar=0}
{
$1=""
count[++cnt] = gsub("s","")
$0 = ""
for(i=1; i<=cnt;i++){
$i=count[i]
if(count[i]>sugar)
sugar=count[i]
}
}
END{
print $1" "$2" has the largest amount of sugar with" sugar" sugars"
}
我得到的输出:
2 1 has the largest amount of sugar with 3 sugars
期望的输出:
Orange pie has the largest amount of sugar with 3 sugars
我用来运行 awk 脚本的命令:
awk -f script.awk calories
答案1
使用 GNU awk 并依赖设置为糖s
模式的字段分隔符:
awk -F'\\<s\\>' '
sugar<NF{sugar=NF;pie=$1}
END{print pie "has the largest amount of sugar with " sugar-1 " sugar"}
' file
唯一的语句是设置行中找到的sugar
数量的变量,并设置饼图名称。s
解析文件时,END
将执行该语句并打印所需的字符串。
答案2
您不需要将计数存储在数组中并为每个新值迭代它 - 只需存储当前的最大值及其相应的饼图:
#!/bin/awk -f
BEGIN {
sugar_max = 0;
pie_max = "";
}
{
pie = $1;
$1 = ""; sugar = gsub("s","");
if (sugar > sugar_max) {
sugar_max = sugar;
pie_max = pie;
}
}
END {
printf "%s pie has the largest amount of sugar with %d sugars\n", pie_max, sugar_max;
}
在这种情况下,该BEGIN
块并不是绝对必要的,因为 awk 变量是隐式初始化的。
答案3
怎么样
awk '
function SCNT(FLD) { x = $FLD; return gsub (/s/, "&", $FLD)}
SMX < SC = SCNT(0) - SCNT(1) - SCNT(2) {SMX = SC
LN = $0
}
END {print "Largest sugar count of", SMX, "in", LN
}
' file
Largest sugar count of 3 in Orange pie s s s