[awk] 使用awk中的hash 统计(python dict

2020-05-31  本文已影响0人  Silver_42ac

使用awk 简单统计 文件第二列中字符串出现的次数

awk '{hash[$2]=hash[$2]+1;}END{for( a in hash){printf("%s,%d\n",a,hash[a]);}}' wrong_id

for a in dict 循环 用法很像是python 的dict 用法,或者shell 中的 for a in *.txt do do samething done

遍历字典(hash,dict)方式
awk:

for (a in  hash){
   do do samething
}

perl:

foreach my $el (keys %hash){
     print  "$el\t$hash[$el ]\n";
}

python:

for  a in dict:
    print (a+"\t"+str(dict[a])+"\n")
    print("\t".join(a,str(dict[a]),"\n"))

shell:

for  fh in *.txt ;do
    do do samething
done
上一篇 下一篇

猜你喜欢

热点阅读