对大文件字符进行计数

时间:2022-07-26
本文章向大家介绍对大文件字符进行计数,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

有一列数据的文件,想计算每行数据的重复次数时可以用sort和uniq进行计数:

#cat file
hello
world
friend
hello
world
hello
sort file |uniq -c 

但是当文件过大时,会报错,显示空间不足: sort: write failed: /tmp/sortbDyE0W: No space left on device

这个时候可以通过awk来进行计数:

cat file | awk '{count[$1]++;} END {for(i in count) {print i count[i]}}' 

参考:https://www.cnblogs.com/hider/p/11834706.html