当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 4 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : 怎么查找一份文档中有多少个单词
帖子发表于 : 2015-09-17 10:40 

注册: 2015-09-06 17:19
帖子: 5
系统: Ubuntu
送出感谢: 1
接收感谢: 0 次
我是一个初级的菜鸟,我想用shell脚本做实现查找一份文档中有多少个单词,请大家指教


页首
 用户资料  
 
2 楼 
 文章标题 : Re: 怎么查找一份文档中有多少个单词
帖子发表于 : 2015-09-17 18:10 
头像

注册: 2010-04-10 16:14
帖子: 2903
系统: Arch Linux
送出感谢: 8
接收感谢: 69
只是要统计词数吗?还是要去重?
去重的话我能想到的大概就只能这样了(这里标点符号只处理了空格、逗号和句号而且遇到例如 2.0 这种词,还会造成多算):
代码:
cat test.txt | tr ' ,.' '\n' | sort -u | wc -l


_________________
dz,U,EIS. cies.
dz-cies@github
小香猪杀手@cc98.org


页首
 用户资料  
 
3 楼 
 文章标题 : Re: 怎么查找一份文档中有多少个单词
帖子发表于 : 2015-09-18 10:04 
头像

注册: 2007-09-29 3:09
帖子: 5777
送出感谢: 0 次
接收感谢: 5
代码:
$ wc --help
Usage: wc [OPTION]... [FILE]...
  or:  wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  A word is a non-zero-length sequence of
characters delimited by white space.

With no FILE, or when FILE is -, read standard input.

The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
      --files0-from=F    read input from the files specified by
                           NUL-terminated names in file F;
                           If F is - then read names from standard input
  -L, --max-line-length  print the maximum display width

-w, --words print the word counts
代码:
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/wc>
or available locally via: info '(coreutils) wc invocation'


_________________
HUNT Unfortunately No Talent...


页首
 用户资料  
 
4 楼 
 文章标题 : Re: 怎么查找一份文档中有多少个单词
帖子发表于 : 2015-09-23 10:04 

注册: 2015-09-23 9:26
帖子: 4
送出感谢: 0 次
接收感谢: 0 次
HuntXu, 正解。
统计通常是用wc指令。
比如新建一个test.txt文件,输入以下内容:
this is line 1
你只需要指令如下指令:
代码:
wc test.txt

就可以得到类似以下的输出:
代码:
1 4 15 test.txt

其中行数是1,单词个数是4,字节数是15。
若只想得到单词个数,如下指令即可:
代码:
wc -w test.txt


PS: 也许你觉得1不是一个单词,但是wc只是通过空格来区分,所以认为此处的1也是一个单词。


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 4 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 0 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译