linux 统计文件中单词出现次数
2014-05-28 17:22
274 查看
[请教]统计文件a.txt中“每个单词”的重复出现次数?若该文件大到几个G又该如何处理?
方案一:
#!/bin/sh
#定义源文件和临时文件
srcfile=word.txt
tempfile_words=tempfile_words
tempfile_words_uniq=tempfile_words_uniq
#取出所有单词,存入临时文件$tempfile_words,一行一个单词
#去除$tempfile_words中重复单词,并把换行符替换为空格,存入临时文件$tempfile_words_uniq
tr "[\015]" "[\n]"<$srcfile|sed 's/[^0-9a-zA-Z ]*\([0-9a-zA-Z]*\)[^0-9a-zA-Z]*/\1\n/g'|sed '/^$/d'>$tempfile_words
sort $tempfile_words|uniq|tr "[\n]" "[ ]">$tempfile_words_uniq
#遍历所有单词,统计数目
words=$(cat $tempfile_words_uniq)
for word in $words
do
word_num=$(grep $word $tempfile_words|wc -l)
echo $word $word_num
done
方案二:
tr -s "\t| " "\n" < filename | sort | uniq -c | sort -n -k 1 -r
方案一:
#!/bin/sh
#定义源文件和临时文件
srcfile=word.txt
tempfile_words=tempfile_words
tempfile_words_uniq=tempfile_words_uniq
#取出所有单词,存入临时文件$tempfile_words,一行一个单词
#去除$tempfile_words中重复单词,并把换行符替换为空格,存入临时文件$tempfile_words_uniq
tr "[\015]" "[\n]"<$srcfile|sed 's/[^0-9a-zA-Z ]*\([0-9a-zA-Z]*\)[^0-9a-zA-Z]*/\1\n/g'|sed '/^$/d'>$tempfile_words
sort $tempfile_words|uniq|tr "[\n]" "[ ]">$tempfile_words_uniq
#遍历所有单词,统计数目
words=$(cat $tempfile_words_uniq)
for word in $words
do
word_num=$(grep $word $tempfile_words|wc -l)
echo $word $word_num
done
方案二:
tr -s "\t| " "\n" < filename | sort | uniq -c | sort -n -k 1 -r
相关文章推荐
- Linux命令经典面试题:统计文件中出现次数最多的前10个单词
- linux中sort(统计文件中出现次数最多的前10个单词)
- sort +awk+uniq 统计文件中出现次数最多的前10个单词
- 统计文件中出现次数最多的前10个单词
- 统计一篇英文文件中,单词出现的次数,并按单词的长度进行排序
- 【代码】统计文件中,不同字符出现的次数(可排序,没有处理英文单词、数字和大小写)
- linux查找某个文件中单词出现的次数
- 针对一个文件 统计其中的单词和字符数 找出文件中出现次数最多的单词数 将文件中出现的单词按频率进行排序并输出
- UNIX/Linux里统计文件里某个字符出现的次数(转)
- linux统计文件中字符串出现次数
- 统计输出某个文件中每个单词出现的次数
- 一道笔试题:统计文件中的单词出现次数
- 统计文本文件中单词出现次数最多的单词
- 统计文件中某个单词出现的次数
- Linux下统计文本文件中前n个出现频率最高的单词
- 统计文件中每个单词出现的次数
- Hadoop 统计文件中某个单词出现的次数
- 统计文件中出现次数最多的前10个单词
- 黑马程序员——统计文件中每个英文单词出现的次数
- sort +awk+uniq 统计文件中出现次数最多的前10个单词