Linux Shell编程实战---统计特定文件中单词的词频
2017-09-08 11:52
211 查看
方法1:使用sedShell>cat a1.txt 123a123,555456.333566。555!88,thisis a good boy.
Shell>cat a1.txt|sed 's/[[:space:]|[:punct:]]/\n/g'|sed '/^$/d'|sort|uniq -c|sort -n-k1 -r 2 555 1 this 1 is 1 good 1 boy 1 a123 1 a 1 88 1 566 1 456 1 333 1 123Shell>
sed 's/[[:space:]|[:punct:]]/\n/g'[]表示正则表达式集合,[:space:]代表空格。[:punct:]代表标点符号。[[:space:]|[:punct:]]代表匹配空格或者标点s/[[:space:]|[:punct:]]/\n/g代表把空格或标点替换成\n换行符
sed '/^$/d' 删除掉空行
方法2:使用awk#!/bin/bash
filename=$1
cat$filename|awk '{ #getline var; split($0,a,/[[:space:]|[:punct:]]/); for(i in a) { word=a[i]; b[word]++; }} END{ printf("%-14s%s\n","Word","Count"); for(i in b) { printf("%-14s%d\n",i,b[i])|"sort-r -n -k2"; }
}'运行结果[root@Test01awk]# cat a1.txt 123a123,555456.333566。555!88,thisis a good boy.
[root@Test01awk]# ./word_freq.sh a1.txt Word Count555 2this 1is 1good 1boy 1a123 1a 188 1566 1456 1333 1123 1 1[root@Test01awk]#
方法3:使用tr[root@Test01awk]# cat a1.txt 123a123,555456.333566i555!88,this is a good boy.
[root@Test01awk]# cat a1.txt |tr '[:space:]|[:punct:]' '\n'|tr -s '\n'|sort|uniq -c|sort -n-k1 -r 2 555 1 this 1 is 1 good 1 boy 1 a123 1 a 1 88 1 566i 1 456 1 333 1 123[root@Test01awk]#
Shell>cat a1.txt|sed 's/[[:space:]|[:punct:]]/\n/g'|sed '/^$/d'|sort|uniq -c|sort -n-k1 -r 2 555 1 this 1 is 1 good 1 boy 1 a123 1 a 1 88 1 566 1 456 1 333 1 123Shell>
sed 's/[[:space:]|[:punct:]]/\n/g'[]表示正则表达式集合,[:space:]代表空格。[:punct:]代表标点符号。[[:space:]|[:punct:]]代表匹配空格或者标点s/[[:space:]|[:punct:]]/\n/g代表把空格或标点替换成\n换行符
sed '/^$/d' 删除掉空行
方法2:使用awk#!/bin/bash
filename=$1
cat$filename|awk '{ #getline var; split($0,a,/[[:space:]|[:punct:]]/); for(i in a) { word=a[i]; b[word]++; }} END{ printf("%-14s%s\n","Word","Count"); for(i in b) { printf("%-14s%d\n",i,b[i])|"sort-r -n -k2"; }
}'运行结果[root@Test01awk]# cat a1.txt 123a123,555456.333566。555!88,thisis a good boy.
[root@Test01awk]# ./word_freq.sh a1.txt Word Count555 2this 1is 1good 1boy 1a123 1a 188 1566 1456 1333 1123 1 1[root@Test01awk]#
方法3:使用tr[root@Test01awk]# cat a1.txt 123a123,555456.333566i555!88,this is a good boy.
[root@Test01awk]# cat a1.txt |tr '[:space:]|[:punct:]' '\n'|tr -s '\n'|sort|uniq -c|sort -n-k1 -r 2 555 1 this 1 is 1 good 1 boy 1 a123 1 a 1 88 1 566i 1 456 1 333 1 123[root@Test01awk]#
相关文章推荐
- 编程统计一个英文文本文件中单词词频
- Linux Shell编程实战---计算特定目录下前10个大文件
- 统计特定文件中的词频
- 统计特定文件中的词频
- python---字符串的拼接、去重、反转、字母花样排序、单词出现判断、统计文件特定单词频率lambda、硬盘容量、列表转字符串
- python---实战3统计特定时间次数(文件操作、替换、列表、排序、去重、元组、集合)
- 练习2-4:编一个程序统计文件中特定单词出现的次数(要求使用string类的运算符==来查找单词) .
- Linux shell 脚本攻略之统计文件的行数、单词数和字符数
- python读取文件里的单词,统计词频,输出到文件
- linux shell 下载维基百科特色条目并统计单词词频
- Shell编程——统计特定目录下文件个数
- 练习2-4:编一个程序统计文件中特定单词出现的次数(要求使用string类的运算符==来查找单词)
- Java编程:统计文本文件中单词出现频率
- 《GPU高性能编程-CUDA实战》中例子头文件使用
- 单词词频统计降序排序(代码贴)
- 给定一个英文原文,统计文件里面一共有多少个不同的英文单词
- c语言统计从文件读取的文章中所有单词的出现次数
- Ruby中实现统计文件行数、单词数和字符数
- 【python 编程】网页中文过滤分词及词频统计