![如何在 awk 脚本中显示另一个文件中两个字段的某些部分](https://linux22.com/image/165783/%E5%A6%82%E4%BD%95%E5%9C%A8%20awk%20%E8%84%9A%E6%9C%AC%E4%B8%AD%E6%98%BE%E7%A4%BA%E5%8F%A6%E4%B8%80%E4%B8%AA%E6%96%87%E4%BB%B6%E4%B8%AD%E4%B8%A4%E4%B8%AA%E5%AD%97%E6%AE%B5%E7%9A%84%E6%9F%90%E4%BA%9B%E9%83%A8%E5%88%86.png)
我有以下文件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