此脚本从第一列中查找重复条目,并从第二列组中打印条目.我想知道脚本是如何实现这一点的.
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)
{
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)
| 归档时间: |
|
| 查看次数: |
79 次 |
| 最近记录: |