最近在看shell中有个题目为统计单词的个数,使用了awk功能,代码如下

#!/bin/bash
if [ $# -ne ];then
echo "Usage:basename $0 filename"
exit
fi
filename=$
egrep -o "[a-zA-Z]+" $filename |
awk '{count[$0]++}
END{printf "%-14s %s\n","Word","Count"
for(i in count)printf "%-14s %s\n",i,count[i]|"sort -nrk 2"}'

使用正则来匹配,+表示1个多个

结果如下:

[root@localhost shellcookbook]# sh word_freq.sh item.txt

Word Count

Tennis

Sports

Racket

Printer

Office

Laser

Video

Refrigerator

Player

MP

HD

Camcorder

Audio

Appliance

正好在学习python,顺便拿python实现一下吧,代码如下:

#!/usr/bin/env python
import sys,re
if len(sys.argv[0:]) != 2:
print "Usage:%s file" % sys.argv[0]
sys.exit(0)
try:
filename=sys.argv[1]
with open(filename) as f:
data=f.read()
except IOError:
print "Please check %s is Exised!" % filename
exit(0)
except Exception,e:
print e
sys.exit()
patten=r'[a-zA-Z]+'
words=re.findall(patten,data)
#print sorted([(i,words.count(i)) for i in set(words)],cmp=lambda x,y:cmp(x[1],y[1]),reverse=True)
wordcounts=sorted([(i,words.count(i)) for i in set(words)],key=lambda x:x[1],reverse=True)
print "%-14s %s" % ("Word","Counts")
for word,counts in wordcounts:
print "%-14s %s" % (word,counts)

使用的也是正则先匹配出来后,再用sorted进行排序并计算出来个数,结果如下:

[root@localhost shellcookbook]# python word_freq_py.py item.txt

Word Counts

Printer 2

Laser 2

Office 2

Tennis 2

Sports 2

Racket 2

Appliance 1

Player 1

Video 1

HD 1

Audio 1

Camcorder 1

Refrigerator 1

MP 1

我们来看看这二个对比,程序效率如何:

# time sh word_freq.sh item.txt
real 0m0.007s
user 0m0.003s
sys 0m0.005s
time python word_freq_py.py item.txt
real 0m0.035s
user 0m0.031s
sys 0m0.004s

对比来看,shell程序更快,主要是使用了awk提高了效率。所以在linux下写的小程序时,shell能实现,还是使用shell实现,python辅助。

使用tuple统计文件中单词的个数

name = input("Enter file:") if len(name) < 1 : name = "input.txt" fhand = ope ...

学c语言做练习之&ZeroWidthSpace;统计文件中字符的个数

统计文件中字符的个数(采用命令行参数) #include #include int main(int argc, char *argv[] ...

JAVA实验--统计文章中单词的个数并排序

分析: 1)要统计单词的个数,就自己的对文章中单词出现的判断的理解来说是:当出现一个非字母的字符的时候,对前面的一部分字符串归结为单词 2)对于最后要判断字母出现的个数这个问题,我认为应该是要用到ma ...

C语言算法--统计字符串中单词的个数

#include #include #include int main(void) { int le ...

Python 统计文本中单词的个数

1.读文件,通过正则匹配 def statisticWord(): line_number = 0 words_dict = {} with open (r'D:\test\test.txt',enc ...

Linux统计文件中单词出现的次数

grep -E "\b[[:alpha:]]+\b"  /etc/fstab  -o | sort | uniq -c 或 awk '{for(i=1;i

Scala快速统计文件中特定单词,字符的个数

val fileContent=Source.fromFile("/home/soyo/桌面/ss5.txt").getLines.mkString(",") ...

sort &plus;awk&plus;uniq 统计文件中出现次数最多的前10个单词

实例cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sort -k1nr|head -100 统计文件中出现次数最多的前10个单 ...

java统计文本中单词出现的个数

package com.java_Test; import java.io.File; import java.util.HashMap; import java.util.Iterator; imp ...