我有一个像这个example.txt的大文本文件:
http://www.fullbooks.com/The-Jacket-Star-Rover-1.html
使用awk命令:
cat example.txt | awk '{ print substr($0, index($0,$3)) }' | tr -sc "[A-Z][a-z][0-9]'" '[\012*]' | awk -- 'first!=""&&second!="" { print first,second,$0; } { first=second; second=$0; }' | sort | uniq -c | sort -nr | head -n20
输出结果是最常出现的三个连续单词的前20名排名:
13 in the jacket
11 I was a
10 of the Yard
10 me in the
8 Captain of the
7 times and places
7 the Captain of
7 in the prison
7 in the dungeons
7 in San Quentin
7 I had been
6 other times and
6 hours in the
6 are going to
5 twenty four hours
5 to take me
5 the rest of
5 the forty lifers
5 the Board of
5 that I had
从以下开始:
raw=open('examples.txt')
text=raw.read().replace('\n', '')
words = text.split()
...............
如何使用Python3获得相同的结果?