Ins*_*too 2 grep sed awk numeric-data
一个文件包含 5 列数字
例子:
12 34 67 88 10
4 90 12 10 7
33 12 5 76 34
Run Code Online (Sandbox Code Playgroud)
我想打印相同的数字,看看它消失了多少次。例子:
3 : 12
2 : 34
Run Code Online (Sandbox Code Playgroud)
此awk
脚本打印输出,如您的示例所示:
awk '{
for ( i=1; i<=NF; i++ ) # loop over all fields/columns
dict[$i]++; # count occurrence in an array using the field value as index/key
}
END { # after processing all data
for (key in dict) # iterate over all array keys
if(dict[key]>1) # if the key occurred more than once
print dict[key] " : " key # print counter and key
}' inputfile
Run Code Online (Sandbox Code Playgroud)
对于示例输入,输出为
2 : 10
3 : 12
2 : 34
Run Code Online (Sandbox Code Playgroud)
如果您删除条件,if(a[i]>1)
它还会列出仅出现一次的数字。
如果要按出现次数的降序对结果进行排序,请追加
| sort -nr
Run Code Online (Sandbox Code Playgroud)
这意味着以相反的数字顺序排序。
所以awk
上面显示的命令结合了 sort
awk '...' inputfile | sort -nr
Run Code Online (Sandbox Code Playgroud)
产生
3 : 12
2 : 34
2 : 10
Run Code Online (Sandbox Code Playgroud)
正如在 glenn jackman 的评论中提到的,您可以for
通过PROCINFO["sorted_in"] = "@val_num_desc"
在END
块的顶部添加来指示 GNU AWK 在处理时对数组值进行排序。
END { # after processing all data
# In GNU AWK only you can use the next line to sort the array for processing
PROCINFO["sorted_in"] = "@val_num_desc" # sort descending by numeric value
for (key in dict) # iterate over all array keys
if(dict[key]>1) # if the key occurred more than once
print dict[key] " : " key # print counter and key
}
Run Code Online (Sandbox Code Playgroud)
使用此 GNU 特定扩展,您无需管道即可获得排序结果sort
。
你可以使用管道
tr -s ' ' '\n' < datafile | sort | uniq -c -d
Run Code Online (Sandbox Code Playgroud)
根据您希望答案的精炼程度,您可以过滤数值。删除-d
以查看所有值,而不仅仅是计数大于 1 的值。