关于awk的二维数组的使用。

mylove0623 2008-12-26 04:48:38
地址是:http://blog.chinaunix.net/u/4136/showart.php?id=30486
可是我怎么测试不好使呢。
##############1.awk
刚学awk。代码就完全拷贝的。我啥也没动啊。。郁闷呢。。。就是不好使。谁帮我看看。解决50全部送你。
...全文
797 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
fox000002 2008-12-26
  • 打赏
  • 举报
回复
结果

Generated at --- Fri Dec 26 16:59:54 CST 2008
The earliest day of June,18th is your choice to invite the most classmates in Shanhai!
They are: Long Zhang Dai
fox000002 2008-12-26
  • 打赏
  • 举报
回复
上面一个文件 1.txt


Fan 0 0 1
Gong 0 1 0
Tian 0 1 1
Long 1 0 0
Zhang 1 0 1
Dai 1 1 0
Jin 1 1 1


下面是脚本

BEGIN {
# RS = ";"
# FS = ")"
printf (" Generated at --- ")
system("date")
printf (" ");
max = 0
date = 2;
}

{#get date and initial total
if ($0 ~ /Name/)
for(x=2; x<=NF; x++) {
total[x, 2] = "They are: "
total[x, 1] = 0 #number of ava members
total[x, 0] = $x #invitation date
}
}

{ if($0 ~ /Jin/) next } #ignore host Mr.Jin

{
for ( x = 2; x <= NF; x++)
if($x == 1) {
total[x, 1] = total[x, 1] + 1
total[x, 2] = sprintf("%s %s",total[x, 2], $1)
#printf ("total[%d] = %d ",x,total[x,1])
if(total[x, 1] > max) {
date = x;
max = total[x, 1]
}
else if ((total[x, 1] == max) && (date > x))
date = x
}
}

END {
#print max, date
if(max == 0)
print "Disappointed result: I am afraid that King's second invitation is to be canceled again! "
else {
print "The earliest day of June,18th is your choice to invite the most classmates in Shanhai!"
print total[date, 2]
}
}



运行时,用


awk -f 1.awk 1.txt

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧