有人可以解释这个Awk脚本吗?

0 bash awk gawk

此脚本从第一列中查找重复条目,并从第二列组中打印条目.我想知道脚本是如何实现这一点的.

awk '{c[$1]++; k[$1]=k[$1] " " $2} END {for (i in c) {if (c[i]>1) print k[i]}}'
Run Code Online (Sandbox Code Playgroud)

Jam*_*own 6

{
    c[$1]++             # count occurances of first field entries
    k[$1]=k[$1] " " $2  # catenate second fields for recurring entries
  # k[$1]=k[$1] $2 " "  # this way output'd look better
} 
END {                   # after counting and catenating
    for (i in c) {      # go thru all entries
        if (c[i]>1)     # and print the catenated second fields for those
            print k[i]  # recurring first fields
    }
}
Run Code Online (Sandbox Code Playgroud)

例如:

key1 data1
key1 data2
key2 data3
Run Code Online (Sandbox Code Playgroud)

会产生输出:

 data1 data2
Run Code Online (Sandbox Code Playgroud)